I am using ASP.Net Web Api 2.
I have almost gone through more than 10-15 links including MSDN but still issue persists, hence posting this question.
<system.webServer>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Request-Headers:" value="*" />
<add name="Access-Control-Request-Method:" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
- Added above configuration in my web.config
- Installed
Microsoft.AspNet.WebApi.Cors
- Decorated my class with
[EnableCors(origins: "*", headers: "*", methods: "*")]
Now I have published this to IIS and trying to access the services from JavaScript/jQuery. However, HTTP Get
is returning success(as desired) but HTTP Post
is returning the below error:
HTTP 405-Method not allowed
What should I do to fix this error for the HTTP Post
or PUT
?
//jQuery ajax call
$.ajax(
{
url : "http://myip:myport/api/Customer
type : "POST",
data: {'custName' : name},
success : function(e)
{
},
error: function(e)
{
}
})
My class:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class CustomerController : ApiController
{
private static List<string> namesList = new List<string>();
public IEnumerable<string> Get()
{
namesList.Add("Chetan");
return namesList;
}
[HttpPost]
public void Post(string name)
{
namesList.Add(name);
return;
}
}
routes.config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Below link doesn't suffice the question. I already have tried with it.
Asp.NET Web API - 405 - HTTP verb used to access this page is not allowed - how to set handler mappings