当在网址中+时,asp mvc 404

时间:2011-10-15 11:47:31

标签: asp.net-mvc-routing http-status-code-404 asp.net-mvc

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

如果我提出此请求测试/详情/ + ,我会 404:测试/详细信息/ +

这是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
);

1 个答案:

答案 0 :(得分:1)

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

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>