当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net web api2 - PUT and DELETE requets are not working in Web API 2 even I have enabled cors

问题描述:

I have an angularjs web app. I have integrated this web api there. Get and Post

requests are working but when I am trying to Put and Delete requests It is giving an error :

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

/#/Delete:1 XMLHttpRequest cannot load http://abc.example.net/api/employees/3. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.net' is therefore not allowed access. The response had HTTP status code 405.

I have written this below code in WebApiConfig.

public static class WebApiConfig

{

public static void Register(HttpConfiguration config)

{

// Web API configuration and services

var cors = new EnableCorsAttribute("http://example.net", "*", "*");

config.EnableCors(cors);

// Configure Web API to use only bearer token authentication.

config.SuppressDefaultHostAuthentication();

config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

// Web API routes

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(

name: "DefaultApi",

routeTemplate: "api/{controller}/{id}",

defaults: new { id = RouteParameter.Optional }

);

}

}

网友答案:

Use a DynamicPoliceProviderFactory class to add all subdomains to the CORS policy or change first parameter of EnableCorsAttribute to * to allow all Origins.

分享给朋友:
您可能感兴趣的文章:
随机阅读: