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.




About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

6 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.

אריה טוכפלד said...

Thanks for the summary.
I used your last suggestion.

karras said...

In IIS 8.5 (.Net 4.6) the path should be without first slash: path="products/*"

Raju said...

Thanks for posting this. But when I use this my POST methods are not working. Any suggestion/help?

Suprotim Agarwal said...

@Raju MVC 4 or 5?