8

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
andreialecu
  • 3,639
  • 3
  • 28
  • 36
  • 3
    May I ask why this is important? As far as browsers are concerned, they don't care. And as far as building your own route urls based on this route, the trailing / will be added automagically when needed. – Tomas Aschan Jun 22 '09 at 12:50
  • I just want to keep my urls consistent with an older version of the site which used urls with a trailing slash. I know in theory it doesn't matter, but for legacy it does. :) – andreialecu Jun 22 '09 at 13:06
  • 1
    Still, there is really no reason to bother - even for legacy. If the trailing slash is all that differs, there is no difference. All old urls will be valid, and all new urls will work with the old locations. – Tomas Aschan Jun 22 '09 at 13:11
  • 4
    Right, but it would have duplicate url issues for SEO. Old bookmarks and search engine results would still use the old url with the slash. Even if I 301 the slash to the nonslash version, it's still a problem that requires some extra code to fix. – andreialecu Jun 22 '09 at 13:36
  • Ok, I resorted to using the urls without a slash. Fixing it to produce the trailing slash would have added unnecessary complexity to the code. Hopefully a future version of ASP.NET MVC will make this possible. – andreialecu Jun 22 '09 at 22:10
  • 1
    Just on the the subject that trailing slashes don't matter... In this case that is true but in many other cases they do matter so I wouldn't take that statement as gospel. – Charlino Jun 23 '09 at 01:21
  • 1
    @Tomas Lycken - this is very important. If your URL's were indexed before with a trailing slash, you're in for a world of hurt trying to 301 all the old URL's to the new, no-slash URL's. If you don't, your pagerank takes a beating because google penalizes for duplicate content at two separate URL's. – womp Jun 24 '09 at 02:55
  • From Google Webmaster Central blog http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html : "If both slash and non-trailing-slash versions contain the same content and each returns 200, you can [lists some options, including] leave it as-is. Many sites have duplicate content. Our indexing process often handles this case for webmasters and users. While it’s not totally optimal behavior, it’s perfectly legitimate and a-okay." – Eric King Feb 20 '13 at 21:58

2 Answers2

4

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

Community
  • 1
  • 1
womp
  • 115,835
  • 26
  • 236
  • 269
-5

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
George Filippakos
  • 16,359
  • 15
  • 81
  • 92