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.)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With