在最新的MVC预览中,我使用此路由作为旧网址:
- routes.MapRoute(
- "Legacy-Firefox",// Route name
- "Firefox-Extension/",// URL with parameters
- new { controller = "Home",action = "Firefox",id = "" } // Parameter defaults
- );
问题是这两个URL的工作:
http://example.com/Firefox-Extension
http://example.com/Firefox-Extension/
我只想要第二个工作(对于SEO).此外,当我创建一个链接到该页面,路由引擎给我一个没有尾部斜线的URL.
- <%= Html.ActionLink("Firefox Extension","Firefox","Home")%>
我相信可以通过使用HTTP处理程序来修改第一个问题,使用尾部的斜杠进行301重定向到URL.但是,我想链接到尾部斜线的URL,我希望不必用斜杠硬编码版本.
任何人都知道如何强制路线使用尾部斜线?
解决方法
如果您在RouteLink上有一个包装器,那么可以解决问题.
例如,我有一个包装方法RouteLinkEx:
例如,我有一个包装方法RouteLinkEx:
- public static string RouteLinkEx(this HtmlHelper helper,string text,string routeName,RouteValueDictionary rvd,object htmlAttributes)
- {
- UrlHelper uh = new UrlHelper(helper.ViewContext.RequestContext,helper.RouteCollection);
- // Add trailing slash to the url of the link
- string url = uh.RouteUrl(routeName,rvd) + "/";
- TagBuilder builder = new TagBuilder("a")
- {
- InnerHtml = !string.IsNullOrEmpty(text) ? HttpUtility.HtmlEncode(text) : string.Empty
- };
- builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
- builder.MergeAttribute("href",url);
- return builder.ToString(TagRenderMode.Normal);
- //---
- }