asp mvc 404 当 +在网址中

发布于 2024-12-10 05:14:57 字数 1604 浏览 0 评论 0原文

我的 asp mvc 3 应用程序有问题。当我将 + 字符放入网址中时,我总是收到 404 错误。所有请求都是ajax get请求。

如果我发出此请求 Test/Details/+ 我得到 404: Test/Details/+

这是 fiddler 中的请求: GET /Test%2FDetails%2F% 2B?t=1318678807718 HTTP/1.1

这里是路由。

routes.MapRoute(
    "PagingTwoTest", // Route name
    "{controller}/{action}/{tag}/p{currentPage}/p{secCurrentPage}/{*term}", // URL with parameters
    new { secCurrentPage = UrlParameter.Optional, term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+", secCurrentPage = "\\d+" }
);

routes.MapRoute(
    "PagingTwo", // Route name
    "{controller}/{action}/p{currentPage}/p{secCurrentPage}/{*term}", // URL with parameters
    new { secCurrentPage = UrlParameter.Optional, term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+", secCurrentPage = "\\d+" }
);

routes.MapRoute(
    "Paging", // Route name
    "{controller}/{action}/p{currentPage}/{*term}", // URL with parameters
    new { term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "DefaultName", // Route name
    "{controller}/{action}/{*id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

I have problem with asp mvc 3 application. When I put the + character in a url I always get a 404 error. All requests are ajax get request.

If I make this request Test/Details/+ I get 404: Test/Details/+

This is request in fiddler: GET /Test%2FDetails%2F%2B?t=1318678807718 HTTP/1.1

Here are routes.

routes.MapRoute(
    "PagingTwoTest", // Route name
    "{controller}/{action}/{tag}/p{currentPage}/p{secCurrentPage}/{*term}", // URL with parameters
    new { secCurrentPage = UrlParameter.Optional, term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+", secCurrentPage = "\\d+" }
);

routes.MapRoute(
    "PagingTwo", // Route name
    "{controller}/{action}/p{currentPage}/p{secCurrentPage}/{*term}", // URL with parameters
    new { secCurrentPage = UrlParameter.Optional, term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+", secCurrentPage = "\\d+" }
);

routes.MapRoute(
    "Paging", // Route name
    "{controller}/{action}/p{currentPage}/{*term}", // URL with parameters
    new { term = UrlParameter.Optional }, // Parameter defaults
    new { currentPage = "\\d+" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
    "DefaultName", // Route name
    "{controller}/{action}/{*id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2024-12-17 05:14:57

在你的限制中,你没有考虑当有人输入一个字符串时你总是请求一个数字,所以我认为你可以尝试:

routes.MapRoute(
    "TestDetails", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { 
        controller = "Test", 
        action = "Details", 
        id = UrlParameter.Optional 
    }
);

所以在你的 TestController 中你可以请求一个字符串:

public class TestController : Controller
{
    ....
    ....

    public ActionResult Details(string? id) //So you can verify if is null
    {
        ViewData["variable"] = id;
        return View();
    }
    ....
    ....
}

在你的 Details.aspx 中你可以这样写:

<%@ Page Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Title, what you want
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p> Blah, blah, blah, you request this: <%: ViewData["variable"] %> </p>
</asp:Content>

In your constraints you are not considering when someone enters a string you allways request a digit, so I think you can try with:

routes.MapRoute(
    "TestDetails", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { 
        controller = "Test", 
        action = "Details", 
        id = UrlParameter.Optional 
    }
);

So in your TestController you can request a string:

public class TestController : Controller
{
    ....
    ....

    public ActionResult Details(string? id) //So you can verify if is null
    {
        ViewData["variable"] = id;
        return View();
    }
    ....
    ....
}

And in your Details.aspx you can put something like this:

<%@ Page Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Title, what you want
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <p> Blah, blah, blah, you request this: <%: ViewData["variable"] %> </p>
</asp:Content>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文