Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trailing slash on an ASP.NET MVC route

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

The problem is that both of these URL's work: http://example.com/Firefox-Extension http://example.com/Firefox-Extension/

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.

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

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

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?

like image 475
Jason Young Avatar asked Sep 30 '08 18:09

Jason Young


People also ask

How do you fix trailing slashes?

A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.

What is a trailing slash in URL?

Email Subscription. A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

Should I use trailing slash?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.

Should not specify a trailing slash?

The short answer is that the trailing slash does not matter for your root domain or subdomain. Google sees the two as equivalent. But trailing slashes do matter for everything else because Google sees the two versions (one with a trailing slash and one without) as being different URLs.

How do I add a trailing slash to a URL?

So feel free to use <match url=" (.* [^/])" /> instead if you want to add the trailing slash for all paths that don’t have them. In fact, to make this easier yet, you can use URL Rewrite’s existing rule wizard to add the trailing slash.

Do you need custom routes in ASP NET MVC?

In any sufficiently complex ASP.NET MVC project, it is likely you will need to add one or more custom routes which either supplement or replace the conventional MVC route. As we learned in Routing Basics in ASP.NET MVC, the default MVC route mapping is:

How to create an MVC route for a web page handler?

This maps to System.Web.UI.PageHandlerFactory so that ASP.NET MVC has a handle on it and you can create an MVC route for it. This would apply to ASP.NET, PHP, or other frameworks too. Run this appcmd command from the command prompt to create the handlers.

How does route matching work in MVC?

The basic process of route matching was covered in Routing Basics in ASP.NET MVC. As we learned previously, in an MVC application, routes define patterns by which incoming URLs are matched to specific controllers, and actions (methods) on those controllers.


2 Answers

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);
      //---  
      }

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.

like image 158
Murad X Avatar answered Sep 26 '22 16:09

Murad X


I happened across this blog post:

http://www.ytechie.com/2008/10/aspnet-mvc-what-about-seo.html

this morning before running into this question on StackOverflow. That blog post (from the author of this question) has a trackback to this blog post from Scott Hanselman with an answer to this question:

http://www.hanselman.com/blog/ASPNETMVCAndTheNewIIS7RewriteModule.aspx

I was surprised to find no link from here to there yet, so I just added it. :)

Scott's answer suggests using URL Rewriting.

like image 44
Michael Maddox Avatar answered Sep 26 '22 16:09

Michael Maddox