Why is ASP.NET MVC ignoring my trailing slash?

社会主义新天地 提交于 2019-12-01 00:57:46

问题


Consider the following route:

    routes.MapRoute(
        "Service", // Route name
        "service/", // URL with parameters
        new {controller = "CustomerService", action = "Index"} // Parameter defaults
        );

Using Url.Action("Service", "CustomerService") produces an url of /service instead of the expected /service/

Is there any way to get this to work, or do I have to resort to implementing my own routing deriving from RouteBase?


回答1:


Legenden - there is no immediate solution to the problem. You may have run across Jason Young's blog post about the issue, which is very informative. Scott Hanselmann posted a reply to it here, basically stating that he didn't think it was a big deal, and if it is, you can leverage the new IIS7 rewrite module to solve it.

Ultimately though, you might want to look at a solution that was posted by murad on a similar question on StackOverflow: Trailing slash on an ASP.NET MVC route




回答2:


In your page load event add:

Dim rawUrl As String = HttpContext.Current.ApplicationInstance.Request.RawUrl
If Not rawUrl.EndsWith("/") Then
    HttpContext.Current.ApplicationInstance.Response.RedirectPermanent(String.Format("~{0}/", rawUrl))
End If


来源:https://stackoverflow.com/questions/1027027/why-is-asp-net-mvc-ignoring-my-trailing-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!