一个ASP.NET MVC路线尾随斜线 [英] Trailing slash on an ASP.NET MVC route

查看:75
本文介绍了一个ASP.NET MVC路线尾随斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的MVC preVIEW,我使用一个传统的URL这条路线:

In the latest MVC preview, I'm using this route for a legacy URL:

routes.MapRoute(
"Legacy-Firefox", // Route name
"Firefox-Extension/", // URL with parameters
new { controller = "Home", action = "Firefox", id = "" } // Parameter defaults
);

的问题是,这两种的URL的工作:
http://mysite.com/Firefox-Extension
http://mysite.com/Firefox-Extension/

我只想要第二次工作(SEO)。此外,当我创建一个链接到该页面,路由引擎给我回一个网址没有斜线。

I only want the second to work (for SEO). Also, when I create a link to that page, the routing engine gives me back a URL without a trailing slash.

这是code我使用生成的链接:

This is the code I'm using to generate the link:

<%= Html.ActionLink("Firefox Extension", "Firefox", "Home")%>

我相信可以通过使用HTTP处理程序做301重定向到与结尾的斜线的URL解决的第一个问题。不过,我要链接到与结尾的斜线的网址,而且我希望不必硬code与斜线的版本。

I believe can fix the first problem by using an HTTP handler to do a 301 redirect to the URL with the trailing slash. However, I want to link to the URL with the trailing slash, and I'm hoping to not have to hard-code the version with the slash.

任何人都知道如何强制路线使用尾随斜线?

Anyone know how to force the route to use a trailing slash?

推荐答案

如果您对RouteLink包装比有问题的简单的解决方案。
例如,我有一个包装方法RouteLinkEx:

If you have a wrapper over RouteLink than there is an easy solution of the problem. For example, I had a wrapper method 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);
      //---  
      }

当你看到我使用的参数首先生成URL。然后,我添加/在URL的结尾。然后我生成使用这些URL链接完成

As you see I used parameters to generate URL first. Then I added "/" at the end of the URL. and then I generated complete link using those URL.

这篇关于一个ASP.NET MVC路线尾随斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆