6

I'm using AttributeRouting with my Web API (MVC 4).

Why does this work?

    [AcceptVerbs("PUT")]
    [PUT("api/v1/tokens/current")]
    public MemoryToken UpdateToken([FromBody] DeviceTokenViewModel viewModel)
    {...}

And this one does not?

    [PUT("api/v1/tokens/current")]
    public MemoryToken UpdateToken([FromBody] DeviceTokenViewModel viewModel)
    {...}

Error message: The requested resource does not support http method "PUT". Why do I have to explicitly accept the PUT verb?

I'm just confused because something similar with POST works just fine (I don't have to specify the accepted verbs):

 [POST("api/v1/tokens")]
 public MemoryToken CreateToken()
 {...}

From various other posts I believe it has to do with the setting in my web.config. The web server section currently looks like this:

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="AttributeRouting" path="routes.axd" verb="*" type="AttributeRouting.Web.Logging.LogRoutesHandler, AttributeRouting.Web" />
</handlers>

I tried a couple things like removing WebDav and stuff. But nothing worked so far (unless explicitly allowing the PUT verb in the annotation).

Oh, I am using Visual Studios built-in development server.

Thanks guys!

Ingmar
  • 1,525
  • 6
  • 34
  • 51

1 Answers1

6

In this link they describe the POST method as being the default if none of the actions match. So that's why it still works for your CreateToken() method without an HttpPost attribute .

  • You can specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, or HttpPut.
  • Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method.
  • If none of the above, the method supports POST.
AardVark71
  • 3,928
  • 2
  • 30
  • 50
  • Hi there, thanks for your explanations. Yes, I know that I can specify the allowed HTTP methods (see the first snippet). And I thought so too that if I just rename my method from "UpdateToken" to "PutToken" (though this doesn't sound nice), I would't need the [AcceptVerbs] attribute. But I do. Maybe this is due to the AttributeRouting (mentioned in the first sentence of my post). It's actually not a big deal. I can live with setting the [AcceptVerbs] attributes where ever I need them. Won't be that much. I was just hoping I could forget about this by tweaking my web.config a bit. Thanks again! – Ingmar Mar 30 '13 at 04:46
  • @IngmarBode, I tried to reproduce your symptom, but could not--I changed the method name to Put and to PutToken and it worked in both cases. Please retry and consider accepting this answer. – Pete Klein Jun 13 '13 at 18:41