That Pesky “Requested Resource Does Not Support HTTP Method ‘POST’” Error When Using MVC Web API
While working on a project that involved MVC Web API, I ran into a strange issue. I created my controller, registered my routes and decorated my methods with “[HttpPost]”, but the application would still not allow me to do POST requests to my method (HTTP 405 – Method not allowed. The requested resource does not support http method ‘POST’.).
Here’s the Web API Class I was using:
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Eam; using HomeBase.Models;
namespace HomeBase.Controllers { public class CatalogController : ApiControllerBase { [HttpPost] public CatalogItemModel[] GetCatalogItems(string Id) {
} }
Here’s the route registration I was using:
config.Routes.MapHttpRoute(
name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { action = "Index",id = RouteParameter.Optional } );
Going crazy? I thought I was too. Turns out both “System.Web.Http” and “System.Web.Mvc” have an “[HttpPost]” attribute. Since this is an API call, I had to use the “[System.Web.Http.HttpPost]” attribute (look closely at your API route registration and note you are calling “MapHttpRoute” and not “MapRoute” like you would for a non-API MVC Controller).
Here’s the new and improved Web API Class:
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Eam; using HomeBase.Models;
namespace HomeBase.Controllers { public class CatalogController : ApiControllerBase { [System.Web.Http.HttpPost] public CatalogItemModel[] GetCatalogItems(string Id) {
} } }