Allowing dots in ASP.NET MVC paths

URLs with a dot(.) in ASP.NET MVC generate a 404. I have seen many solutions on the web to solve this issue. Some of them are as follows:

1. Add the following handler to your web.config

<system.webServer>
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0_2"
path="/products/*"
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>


Here I am assuming that the url’s that contains dots in your case is starting from /products/urlwith.adot. A drawback is as soon as you set path to [path="*"] all requests to static files such as .pdf, .js, .css etc fails.


2. RunAllManagedModulesForAllRequests

<system.webserver>
<modules runAllManagedModulesForAllRequests="true">

Drawback is performance hit as it enables all managed modules for static requests as well such as for .pdf, .js, .css etc. So everything gets processed by .NET even if it is not required.

3. relaxedUrlToFileSystemMapping

<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

Drawback is that it does not work with newer versions of MVC.

Easiest Solution to allows dots in ASP.NET MVC Paths

One of the easiest solutions to allows dots in path names is the following:

Open IIS Manager (inetmgr) > Go the site or folder

Open URL Rewrite

In the right hand column, from the Action Pane Click on ‘Add Rule(s)’. In the Search Engine Optimization  (SEO) Section, click on “Append or remove the trailing slash symbol” > Choose the default option “Appended if it does not exist” and click ok.

This creates an entry in your web.config file as follows:

<rewrite>
<rules>
<clear />
<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>
</rewrite>

And that’s it. This rule allows dots in MVC paths as well as keeps Search engines happy by creating a redirect rule that will enforce the use of the trailing slash symbol in the URL.

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

3 comments:

Cpt. Kirk said...

Thanks a ton !

i needed all three settings to get it work - nowhere else was all three listed !

mrunwal said...

None of them is working for me.

Aryeh Tuchfeld said...

Thanks for the summary.
I used your last suggestion.