.Net Web.API Cors Put请求失败?

时间:2014-05-12 16:13:59

标签: asp.net-mvc cors put asp.net-web-api2

之前我没有使用过Microsoft web.api而且我无法获得put请求。

我将一个简单的web.api控制器放在一起如下:

[EnableCors(origins: "*", headers: "*", methods: "GET,PUT,POST,DELETE,OPTIONS")]
public class UserEventsController : ApiController
{
    private Repository repository = new Repository();

    public HttpResponseMessage Get()
    {
        var resp = Request.CreateResponse("This will be an event list");
        return resp;
    }

    public HttpResponseMessage Put(string data)
    {
        return Request.CreateResponse(HttpStatusCode.OK, "This will be a PUT response.");
    }

}

使用fiddler中的以下请求和响应,get请求正常工作:

OPTIONS http://localhost:59365/api/userevents HTTP/1.1
Host: localhost:59365
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:57995
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/34.0.1847.131 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:57995/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: content-type
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    QzpcZGF0YVxwcm9qZWN0c1xBcnRlcnlNZWRpYVxQaGlsaXBzLkV2ZW50c1xQaGlsaXBzLkV2ZW50cy5XZWJVSVxhcGl    cdXNlcmV2ZW50cw==?=
X-Powered-By: ASP.NET
Date: Mon, 12 May 2014 15:52:48 GMT
Content-Length: 0

GET http://localhost:59365/api/userevents HTTP/1.1
Host: localhost:59365
Connection: keep-alive
Accept: text/plain, */*; q=0.01
Origin: http://localhost:57995
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)             Chrome/34.0.1847.131 Safari/537.36
Content-Type: application/json
Referer: http://localhost:57995/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    QzpcZGF0YVxwcm9qZWN0c1xBcnRlcnlNZWRpYVxQaGlsaXBzLkV2ZW50c1xQaGlsaXBzLkV2ZW50cy5XZWJVSVxhcGl    cdXNlcmV2ZW50cw==?=
X-Powered-By: ASP.NET
Date: Mon, 12 May 2014 15:54:38 GMT
Content-Length: 28

"This will be an event list"

但是,当我尝试执行put请求时,我得到以下内容:

OPTIONS http://localhost:59365/api/userevents HTTP/1.1
Host: localhost:59365
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://localhost:57995
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/34.0.1847.131 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:57995/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6

HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    QzpcZGF0YVxwcm9qZWN0c1xBcnRlcnlNZWRpYVxQaGlsaXBzLkV2ZW50c1xQaGlsaXBzLkV2ZW50cy5XZWJVSVxhcGl    cdXNlcmV2ZW50cw==?=
X-Powered-By: ASP.NET
Date: Mon, 12 May 2014 15:56:58 GMT
Content-Length: 72

{"Message":"The requested resource does not support http method 'PUT'."}

在Chrome的开发者控制台中,我收到的错误如下:

OPTIONS http://localhost:59365/api/userevents 405 (Method Not Allowed) jquery-    1.9.0.js:8475
XMLHttpRequest cannot load http://localhost:59365/api/userevents. No 'Access-Control-    Allow-Origin' header is present on the requested resource. Origin 'http://localhost:57995'     is therefore not allowed access. 

对put的jquery调用如下:

$.ajax({
            type: "PUT",
            url: servicesBaseUrl,
            contentType: "application/json",
            data: '{ "name": "Tricia","age": "37"}',
            dataType: "text",
            success: function (response) {

                // Put the plain text in the PRE tag.
                $("#putResponse").text(response);

            },
            error: function (error) {

                // Log any error.
                console.log("ERROR:", error);

            },
            complete: function () {

                // When this completes, execute something?


            }
        });

有趣的是,如果我从控制器中删除EnableCors属性并从

更改WebApiConfig类中的cors配置
config.EnableCors();

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

如果获得以下

PUT http://localhost:59365/api/userevents 405 (Method Not Allowed) 
  OPTIONS http://localhost:59365/api/userevents HTTP/1.1
Host: localhost:59365
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://localhost:57995
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/34.0.1847.131 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
Referer: http://localhost:57995/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT
Access-Control-Allow-Headers: content-type
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    QzpcZGF0YVxwcm9qZWN0c1xBcnRlcnlNZWRpYVxQaGlsaXBzLkV2ZW50c1xQaGlsaXBzLkV2ZW50cy5XZWJVSVxhcGl    cdXNlcmV2ZW50cw==?=
X-Powered-By: ASP.NET
Date: Mon, 12 May 2014 16:17:29 GMT
Content-Length: 0

如果回来确定要提前预订? 其次是:

PUT http://localhost:59365/api/userevents HTTP/1.1
Host: localhost:59365
Connection: keep-alive
Content-Length: 31
Accept: text/plain, */*; q=0.01
Origin: http://localhost:57995
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/34.0.1847.131 Safari/537.36
Content-Type: application/json
Referer: http://localhost:57995/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6

{ "name": "Tricia","age": "37"}

HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    QzpcZGF0YVxwcm9qZWN0c1xBcnRlcnlNZWRpYVxQaGlsaXBzLkV2ZW50c1xQaGlsaXBzLkV2ZW50cy5XZWJVSVxhcGl    cdXNlcmV2ZW50cw==?=
X-Powered-By: ASP.NET
Date: Mon, 12 May 2014 16:17:29 GMT
Content-Length: 72

{"Message":"The requested resource does not support http method 'PUT'."}

非常感谢帮助解决这个问题!

干杯

0 个答案:

没有答案