In a previous post, I discussed using the Restful Routing NuGet package with ASP.NET MVC. Well, I use it all the time when working with ASP.NET MVC. I started a new project the other day and found that the
Destroy actions (which use the HTTP verb
DELETE) were giving me an HTTP 404 error. A 404 error struck me as a bit odd, as the problem is often an HTTP 405 (method not allowed) when attempting to perform a
DELETE in ASP.NET projects. What was especially puzzling was that I’ve used Restful Routing so many other times, what was going on with this new project?
One of These Things (Is Not Like The Other)
Ok, so why would one project work and another fail? All of them run through IIS Express, so it probably isn’t a problem there, though if you search around the web for errors relating to
DELETE in .NET, you’ll find a lot of articles related to changing the
applicationHost.config. Anyway, that wasn’t my problem.
I started by looking at the routes for the project. Restful Routing provides an awesome route debugger. Just adding the line
map.DebugRoute("routedebug"); to your
Routes.cs file (I almost wrote
routes.rb there :), GO RUBY!), then you can hit
/routedebug to get a handy list of your routes. Well, the route was clearly there, so that wasn’t the problem.
As a last ditch effort, I figured it had something to do with the
web.config, and sure enough, I was correct. Comparing one web.config to another, I noticed that the problem. I was missing a
modules section, and corrected the issue by running all the requests through .NET by simply setting
<system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>
If you aren’t familiar with the
runAllManagedModulesForAllRequests parameter, the documentation definition is:
True if all managed modules can process all requests, even if the request was not for managed content; otherwise, false.
In other words we’re saying, “Hey IIS, hand everything off to .NET for processing.” In our case IIS was getting in the way.
I didn’t try any explicit
PUT requests (as I use RestfulRouting’s
PutOverrideTag HTML Helper), but given that
DELETE are the two “problem” HTTP verbs, this should work for
PUT calls as well.
In general, I found that most of my projects have had the
runAllManagedModulesForAllRequests attribute set to
true. However, it seems like there should be a more elegant way to solve this problem. I did mess around with IIS a little bit, but didn’t come up with anything to fix the
DELETE 404 error. I would be interested to know if any of you have found a better way to solve this problem. Given that WebAPI uses REST (why ASP.NET MVC doesn’t baffles me), it feels like there must be a better solution.