Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs

One annoyance that some developers have run into with ASP.NET MVC is that certain reserved filenames are not allowed in URLs. Often, this is manifested as a Bad Request error or a File Not Found (404) error.

The specifics of this restriction are accounted for in an interesting blog post entitled Zombie Operating Systems and ASP.NET MVC. This actually wasn’t a restriction on ASP.NET MVC but was built into the core of ASP.NET itself.

Fortunately, ASP.NET 4 fixes this issue with a new setting. In web.config, simply add <httpRuntime relaxedUrlToFileSystemMapping="true"/> to the system.web node. Here’s a snippet from my web.config.

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

    <!-- ... your other settings ... -->
  </system.web>
</configuration>

Here is a screenshot of it working on my machine.

con-in-my-url

Now you are free to use COM1-9, LPT1-9, AUX, PRT, NUL, CON in your URLs. I know you were dying to do so. :)

What about web.config?

So the question comes up from time to time, “what if I want to have web.config in my URL?” Why would you want that? Well if you are StackOverflow.com, this makes sense because of the tagging system which places a tag (such as the “web.config” tag, into the URL. I’m not sure why anyone else would want it. ;)

The answer is yes, it works.

web.config-in-my-url

Please note, that you still can’t request /web.config because that would try to request web.config in the root of your web application and ASP.NET won’t allow that for good reason!

In fact, any request for a *.config file that doesn’t match a route will fail.

While I think the vast majority of developers really won’t encounter this issue, it’s a really improvement included in ASP.NET 4 for those that do care.

Keep in mind that this isn’t restricted to just these special names. For example, a URL segment ending with a dot such as the following URL http://example.com/version/1.0./something will not work unless you set the this web.config value.

Technorati Tags: ,,,

What others have said

Requesting Gravatar... Austin Grigg Apr 29, 2010 2:51 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Phil,

I'm glad you are planning to blog more regularly again now that things have slowed down. Your posts are always very practical and helpful. Thanks.
Requesting Gravatar... Brandon Apr 30, 2010 2:54 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Thanks a ton Phil! Most people would never be affected by this or notice it, but it came up when I had a 3 character state code in my .net mvc url of 'CON' representing 'Cornwall' in the UK. Now off to upgrade to .net 4...
Requesting Gravatar... ahjohannessen Apr 30, 2010 3:42 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Phil, your posts are always funny and witty :) Always enjoy what you post. Happy that MS has you on MVC, bright guy with an open ear :)
Requesting Gravatar... Justin Apr 30, 2010 4:23 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Good to know, although I have never run into this problem. I hope this recent burst of posts is a sign of things to come.
Requesting Gravatar... Eric Apr 30, 2010 5:44 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Does ASP.NET MVC allow for URLs like the following or will the trailing period trip it up?

http://localhost/movies/view/E.T.
Requesting Gravatar... Chris McLeod Apr 30, 2010 7:21 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Fantastic news.

Our (ASP.NET MVC-built) Intranet has to deal with listing projects by three-letter "client codes" - which so happen to include clients with the codes AUX, PRT, NUL and CON! Staff have been unable to view projects by these clients, so this change is very welcome!
Requesting Gravatar... Michael Stum Apr 30, 2010 7:33 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Does that also allow Colons in the Url? Can't test it right now, but the inability of ASP.net to serve a URL like "http://Myserver/Special:Something" really sucks :)
Requesting Gravatar... Dan F Apr 30, 2010 10:15 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
@Michael (or anyone else who googles this up in the future) You probably already know this, but that's a http.sys restriction, rather than an asp.net restriction. You can do some registry hacking to turn it off - SO 667429 links through to KB 820129. It's not really an option if you're not in control of the server though, so hopefully the relaxedUrlToFileSystemMapping helps out here.
Requesting Gravatar... kad1r Apr 30, 2010 5:34 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Hello Phil. I have a question for web.config section. Is it possible to reach web.config like that? Is there any way (security issue)?
Requesting Gravatar... Bradley Landis May 01, 2010 12:45 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
So why is the book called Professional ASP.NET MVC 2.0 vs MVC 2?
Requesting Gravatar... haacked May 01, 2010 1:08 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
@kad1r I addreessed that in my post.

Please note, that you still can’t request /web.config because that would try to request web.config in the root of your web application and ASP.NET won’t allow that for good reason!

You can't request a physical web.config file. Or any file with *.config extension.

@Bradley the official product name is ASP.NET MVC 2 without the decimal. The book as it's listed on Amazon needs a few corrections.
Requesting Gravatar... Jeff Putz May 05, 2010 2:50 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
I'd like to know about Eric's question, regarding the trailing period. I seem to encounter that entirely too frequently.
Requesting Gravatar... Vampal May 06, 2010 12:50 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
"/*.*/" is different with "/*.*"
Requesting Gravatar... Guy May 11, 2010 5:10 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Correct me if I'm wrong, but couldn't you read in the contents of the web.config file and list it as the result of requesting the web.config route?

I don't suggest that you do that but you could "fake it" like that if you want people to think that you are directly listing the contents on the web.config.

This could also be a honey trap for hackers. i.e. list a fake web.config with a DB connection string that just logs and reports attempted hacking.
Requesting Gravatar... Ted Nov 23, 2010 10:17 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
How can we allow this in .Net 2? Updating to 4.0 is not an option for us and we need to allow CON, NUL, AUX etc in our URLS?
Requesting Gravatar... Serhiy Sep 05, 2011 7:27 AM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
And what about IIS 6 and ASP.NET 4.0? I still have this error at production (IIS6) but not at my machine (IIS7) :(
Requesting Gravatar... Rakesh Feb 06, 2012 3:08 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Hello Phil - I guess I may be replying to an old phantom post here, but just in case you read it at some point in time.
Even with this setting in .Net 4.0 this does not work for me. It works in localhost (via VS2010) even without this setting <httpRuntime relaxedUrlToFileSystemMapping="true" /> , but when deployed on a Test server fails.
Requesting Gravatar... Rakesh Feb 06, 2012 4:20 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
Phil - Never mind, I got it to work by mapping the aspnet_isapi.dll to wildcard applicaton maps under the application configuration for my website.
I'm putting this here, in case someone comes here looking for another solution.
Requesting Gravatar... witsml Feb 08, 2012 9:04 PM
# re: Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs
does anyone know if this will allow the url rewrite module to receive urls that end with a period "." ? Currently this is being blocked from any processing, so external urls that accidently added a "." to the end of the url are getting error pages, instead of allowing me to redirect them to the page they actually wanted (and get the benefit of having another functioning inbound link)

What do you have to say?

(will show your gravatar)
Please add 6 and 2 and type the answer here: