Niranjan Godbole
Niranjan Godbole

Reputation: 2175

Cors not working for Delete and Put in web api2

I have been struggling on the CORS issue since one week. I am writing web services using web api2. I created one controller(for basic crud operations) and hosted in server 192.168.0.213:8041/api/user_creation and i created another mvc5 application and hosted in the same server as http://192.168.0.213:8040/usercreation/Index. I have enabled cors using the below link.

http://www.c-sharpcorner.com/UploadFile/009ee3/implementation-of-cross-origin-request-in-Asp-Net-web-api-2/

I am using angularjs to fetch services as below. I able to get data from server and also I am able to push data to server. So my GET and POST verbs are working fine. Whenever i tried to update and delete i am facing issues and getting below error message.

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)  http://192.168.0.213:8041/User_Creation/1020
XMLHttpRequest cannot load http://192.168.0.213:8041/User_Creation/1020. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.213:8040' is therefore not allowed access. The response had HTTP status code 405.

I am using angularjs to call web services as below. For example for update,

this.update = function (sub) {
        var url = 'http://192.168.0.213:8041/User_Creation/' + sub.user_id;
        return $http({
            method: 'put',
            data: JSON.stringify(sub),
            url: url,
            contentType: "application/json"
        });
    }

For delete,

this.deleteSubscriber = function (user_id) {
        var url = 'http://192.168.0.213:8041/User_Creation/' + user_id;
        return $http.delete(url).then(function (response) {
            return response.data;
        });
    }

From Web Api I am receiving data in the form of json so i added below code in global.asx. GlobalConfigGlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings

.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters
            .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

This is my handler code in web.config

<handlers>
      <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>

Below are request and response data.

   Request URL: http://192.168.0.213:8041/User_Creation/1021
   Request Method: DELETE
   Status Code: 405 / Method Not Allowed
 - Request Headers
   Accept: application/json, text/plain, */*
   Accept-Encoding: gzip, deflate
   Accept-Language: en-US
   Cache-Control: no-cache
   Connection: Keep-Alive
   Content-Length: 0
   Host: 192.168.0.213:8041
   Referer: http://192.168.0.213:8040/usercreation/Index
   User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
 - Response Headers
   Allow: GET, HEAD, OPTIONS, TRACE
   Content-Length: 1293
   Content-Type: text/html
   Date: Mon, 16 Jan 2017 08:34:30 GMT
   Server: Microsoft-IIS/8.5
   X-Powered-By: ASP.NET

I am facing problem with PUT and Delete method. May i get some help on this? Can someone tell me where i am doing wrong? Thank you.

Upvotes: 0

Views: 1155

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

It looks like PUT and DELETE verbs are not allowed on your web server and the issue you are having is not related to CORS at all. There are a couple of things you might want to checkout to ensure that those verbs are allowed in the first place. Before trying to make cross domain AJAX calls, make sure that you can make standard HTTP calls to your Web API and that it responds to those verbs.

So in your web.config you might have something like this:

add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Notice the absence of PUT and DELETE verbs here.

Also make sure that you have removed all WebDAV and WebDAVModule traces from your web.config as this might be interfering with your requests:

<modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/>
</modules>

Now that you can successfully call your Web API endpoints using the PUT and DELETE verbs using Postman or Fiddler you may go back to the javascript attempts.

Upvotes: 2

Related Questions