Thursday, October 11, 2018

Getting error 405 (Method Not Allowed) while calling POST method of WEBAPI from Angular

Issue is due CORS, it should be enabled in WEBAPI project. The CORS enabling not works through WEB.config code, you need to enable CORS through backend.
Note - remove all web.config CORS enabling code if its there in web.config i.e the below code

    
       name="Access-Control-Allow-Origin" value="*" />
       name="Access-Control-Allow-Headers" value="Content-Type" />
       name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    

Steps 1. Install CORS package Install-Package Microsoft.AspNet.WebApi.Cors for WEBAPI project 2. In WebApiConfig.cs add this code
var corsAttribute = new EnableCorsAttribute("*","Origin, Content-Type, Accept",
                                               "GET, PUT, POST, DELETE, OPTIONS");
            config.EnableCors(corsAttribute);
Done.


No comments: