6

In my ASP.NET MVC 4 application's RouteConfig file, I have registered the following default route:

routes.MapRoute("Default", "{controller}/{action}/{id}",
    new { controller = "home", action = "index", id = UrlParameter.Optional });

Now, in my Razor views, I want to generate a URL to my application's root like that:

<a href="@Url.Action("index", "home")">Home</a>

The generated URL includes a trailing slash; clicking the link thus opens the page localhost/IISApplicationName/. However, I want the URL not to contain the trailing slash so that the URL is localhost/IISApplicationName. Generating routes for other actions, such as /Account/Login, does not create URLs with trailing slashes — it's just about the route linking to my application's root.

Is there any way to prevent ASP.NET MVC routing from appending the trailing slash to the above route?

(I know that I can redirect from the URL including the slash to the one without it, but I'd rather have routing generate the correct route URL in the first place.)

4
  • 1
    I cannot duplicate your problem I use Default route and it goes to url without / Commented Jul 4, 2012 at 8:44
  • 1
    just for information, what's the problem with trailing slash? Commented Jul 4, 2012 at 8:45
  • @alexanderb 1) Both links end up showing the same page despite having slightly different URLs. Not good ... 2) It's ugly ;-). Commented Jul 4, 2012 at 8:47
  • 2
    I think that one could be useful: net.tutsplus.com/tutorials/… Commented Jul 4, 2012 at 8:54

2 Answers 2

1

I recommend URL Rwrite Outbounds rules.

Creating Outbound Rules for URL Rewrite Module : URL Rewrite Module 2 : URL Rewrite Module : The Official Microsoft IIS Site

Sample config:

    <rewrite>
        <outboundRules>
            <rule name="RewriteSlash">
                <match filterByTags="A" pattern="^/IISApplicationName/$" />
                <action type="Rewrite" value="/IISApplicationName" />
            </rule>
        </outboundRules>
    </rewrite>
Sign up to request clarification or add additional context in comments.

Comments

0

Is there any way to prevent ASP.NET MVC routing from appending the trailing slash to the above route?

You should pass a trailing slash when the relative path is empty. If you somehow generate links with href localhost/IISApplicationName still the browser appends a "/" at the end (you can easily verify this in firebug, just type http://stackoverflow.com in the browser address bar and check the requests tab you can see the trailing "/").

This is because as per the HTTP/1.1,

the absolute path cannot be emtpy; if none is present in the original URI, it mut be given as "/" (the server root).

http://www.faqs.org/rfcs/rfc2616.html#ixzz0kGcaRbqU (section 5.1.2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.