ederbf
ederbf

Reputation: 1713

How to allow URLs to contain dots in ASP.NET MVC5?

I am working on a website that should allow dots in URLs, like www.mysite.com/somedata/my.name-123. My desired behaviour is to make that url equivalent to www.mysite.com/somedata/myname-123, but instead I am getting 404 errors because those dots make IIS try to find a static file. A similar question can be found here, but the proposed solutions do not work in my case, as I explain below. This is what I have tried:

I cannot think of any other alternatives. Ideally I would like to add a new handler and make it work for all URLs except those ending with certain patterns (.css, .js).


UPDATE: I have found a solution that works for me. I will try to explain the steps I took:

First of all, I found a really good explanation of why this behaviour is happening: Scott Forsyth's blog. Some of the solutions listed above are also discussed. After rading the article I decided to handle dotted urls with a rewrite rule. Instead of adding a trailing slash I decided to serve the url without the troublesome dots and then check what the user is looking for in the controller of my application. My base rule looks like this one:

<rule name="remove troublesome dots" stopProcessing="false">
    <match url="^(.*)\.(\S*)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}{R:2}" />
</rule>

Note that dots are removed one at a time, so urls with several dots will hit this rule several time. This is bad if urls are likely to have a large number of dots.

If a static file path does not map directly it's disk location, my approach will make it crash. To avoid problems, we need to add exceptions to the remove dots rule for the file endings we need. Seb Nilsson explains how to do it in this blog post, under the "Adding conditions for specific file-endings" section.

Upvotes: 7

Views: 2287

Answers (1)

Matias Cicero
Matias Cicero

Reputation: 26281

I remember confronting this situation some months ago. I was really in a hurry, so what I did may not be the recommended approach.

I specified only one segment, and then created the appropiate regex as a constraint

For instance, it'd end up something like this:

url: somedata/{dotted}

constraints: dotted: "(.*\.)*.*"

That regex is extremely generic, you should reformulate it to accept only supported characters, in order to avoid invalid symbols.

Upvotes: 4

Related Questions