Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 Method Not Allowed Web API 2

I've been through just about every article I can find, on SO and more. I've made changes to the Web.config to meet the answers as they all seem to point to removing the WebDAV module and handler.

However, I'm still getting the error:

405 Method Not Allowed

The requested resource does not support http method 'PUT'.

NOTE: this was originally just an MVC 4 project. I've added artifacts to support Web API. Seems like it's possible I may have missed something.

NOTE: the GET calls are working just fine from Angular.

Web API Route Config

configuration.Routes.MapHttpRoute(
    name: "Default API",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { action = "Get", id = RouteParameter.Optional },
    constraints: new { id = @"[\da-z-]{36}" });

configuration.Routes.MapHttpRoute(
    name: "Default API with Action",
    routeTemplate: "api/{controller}/{action}",
    defaults: new { action = "Get" });

var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

Web.config

<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule" />
</modules>
<handlers accessPolicy="Read, Execute, Script">
  <remove name="WebDAV" />
  <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" />
</handlers>

PUT API Entry

public async Task<IHttpActionResult> Put([FromBody] EditUserViewModel viewModel)

Client Side

vm.model.$update(function () {
    $state.go(userStates.app);
});

Is it possible it's falling into the second route configuration? If so, how can I verify what route configuration .NET is trying to use?

like image 218
Mike Perrenoud Avatar asked Sep 25 '22 22:09

Mike Perrenoud


1 Answers

In the end it appears to be the fact that I was trying to complicate the routing table. I ended up rolling it back to this:

configuration.MapHttpAttributeRoutes();

configuration.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional });

Do take note to the fact that I added configuration.MapHttpAttributeRoutes(); as well.

After those two changes all is working well believe it or not. The Web.config looks like this:

<handlers>
  <remove name="ExtensionlessUrl-Integrated-4.0"/>
  <add name="ExtensionlessUrl-Integrated-4.0"
       path="*."
       verb="*"
       type="System.Web.Handlers.TransferRequestHandler"
       preCondition="integratedMode,runtimeVersionv4.0"
       responseBufferLimit="0" />
</handlers>
like image 69
Mike Perrenoud Avatar answered Oct 11 '22 06:10

Mike Perrenoud