2

I have an ASP.NET MVC 5 controller using attribute routes that doesn't appear to be working with DELETE operations.

 [Route("deletealbum/{id}")]
 [AcceptVerbs(HttpVerbs.Delete)]
 public ActionResult DeleteAlbum(int id)
 {
     // rest of the code omitted here
     return Json(true, JsonRequestBehavior.AllowGet);
 }

I explicitly changed the route name to deletename to rule out other route overload conflicts.

The above action does not fire - I get a 404 not found from IIS. However, if I switch the above action to a HttpVerbs.Get the route is found no problem.

First time in a long time building an API with MVC instead of Web API, and I can't figure out why the route is not firing - it almost looks like any DELETE operations are blocked. I see the same behavior in a different controller - GET works fine, but DELETE operation returns a 404. Is there some configuration setting that needs to be flipped to enable additional verbs perhaps?

Rick Strahl
  • 17,302
  • 14
  • 89
  • 134
  • On what action result is the delete operation taking place? I am asking because in a standard mvc app ( not api , and not json ) the HttpGet has to have the same name as the HttpPost in the route attribute. –  Apr 06 '15 at 09:23
  • @gerdi - Not sure I understand your question. The route is unique (although I really want to be the same as GET/POST/PUT - ie. albums/{id}), but to ensure there's not some route resolution issue I used a unique name. I'm not aware that there are any restrictions on the names in attribute routes based on verbs in MVC. – Rick Strahl Apr 06 '15 at 09:59
  • WebDAV could be the issue, worth checking: http://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8 – tugberk Apr 06 '15 at 10:06
  • Blog post derived from this post: http://weblog.west-wind.com/posts/2015/Apr/09/ASPNET-MVC-HttpVerbsDeletePut-Routes-not-firing – vcsjones Apr 10 '15 at 02:47

1 Answers1

7

This could be due to an IIS configuration issue as outlined in more detail in this answer to a similar question

You may need to enable DELETE (and PUT if you need it) in your IIS config something like this:

Before:

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

After:

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
Community
  • 1
  • 1
Steve Willcock
  • 26,111
  • 4
  • 43
  • 40
  • 1
    Yup that's it! I had added the WebDav workaround but not the extensionless URL. This is a pain - oddly it seems that this was working for Web API apps without explicit configuration changes. – Rick Strahl Apr 06 '15 at 10:15
  • Yes, it is a pain - great that you got it working :) – Steve Willcock Apr 06 '15 at 10:18