代码之家  ›  专栏  ›  技术社区  ›  andreialecu

为什么ASP.NET MVC忽略了我的尾随斜杠?

  •  8
  • andreialecu  · 技术社区  · 15 年前

    考虑以下路径:

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

    使用 Url.Action("Service", "CustomerService") 生成的URL /service 而不是预期的 /service/

    有没有办法让它工作,或者我必须依靠实现我自己的路由 RouteBase ?

    2 回复  |  直到 12 年前
        1
  •  4
  •   Community CDub    8 年前

    legendenden-这个问题没有立即解决的办法。你可能遇到过 Jason Young's blog post 关于这个问题,这是非常有信息性的。 Scott Hanselmann posted a reply 在这里,基本上说他认为这不是什么大问题,如果是的话,你可以利用新的IIS7重写模块来解决它。

    但最终,您可能想看看Murad在StackOverflow上发布的一个类似问题的解决方案: Trailing slash on an ASP.NET MVC route

        2
  •  -5
  •   George Filippakos    13 年前

    在页面加载事件中,添加:

    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