I'm hosting my mvc4 application in a virtual path under an existing site:
ie: http://www.mysite.com/myApp
There is a well-known problem with relative paths being broken if the trailing slash is not provided in the URL. So if the user types the URL as I did above, scripts, styles etc that are using a relative path are not going to be found.
If a trailing slash is provided, everything is ok.
To resolve this problem I installed URL Rewrite (http://www.iis.net/downloads/microsoft/url-rewrite) and added the rule to append the trailing slash if not present. This is a pre-defined rule that looks like:
<rewrite>
<rules>
<rule name="AddTrailingSlashRule1" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
So far so good, the only problem is that this is now breaking the bundles by adding a trailing slash between the bundle name/url and the chache busting:
ie:
http://www.mysite.com/myApp/bundles/myBundle/?v=8_EQPT2vzBgg4HcGhkeTQpLE1flm2VOsp3A1ZEy-C3k1
(Note the / between myBundle and ?v=8....)
I have tried excluding "bundles" from the conditions, but I had no luck.
I would like to know how I can either exclude certain paths from the conditions (not all my bundles are using "bundles" on the path) or maybe a simpler rule that allow me to add the trailing slash for the only case I need to care about: when the user forgets to type it at the end of my app url.
Thanks, R.
UPDATE
I just added a permanent redirection condition to my main controller -the one that is more susceptible to be typed by the user directly on the address bar. This is resolving my issue. I'm letting the question open just in case someone came up with a better idea by either tweaking the route engine or using a URL Rewrite.
This is the code:
public ActionResult Index()
{
if (!Request.Path.EndsWith("/"))
return RedirectPermanent(Request.Url.ToString() + "/");
return View();
}