所请求的资源不支持HTTP方法“选项”。? [英] The requested resource does not support http method 'OPTIONS'.?

查看:4270
本文介绍了所请求的资源不支持HTTP方法“选项”。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的angular.js客户做出如下要求一个asp.net网页API PUT方法:

I am making the following request to an asp.net web api PUT method from my angular.js client:

var org = {
                  OrgId: 111,
                  name: 'testing testing'
          };
$http.put("http://localhost:54822/api/data/putorganisation/1/", org).then(function(status) {
         console.log("success PUT");
         return status.data;
});

但得到以下ERRORMSG(以小提琴手):

However getting the following errormsg (in fiddler):

{"message":"The requested resource does not support http method 'OPTIONS'."}

这是我的asp.net网页API的web.config文件的一部分:

This is a part of my asp.net web api web.config file:

 <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type,x-xsrf-token,X-Requested-With" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
    <validation validateIntegratedModeConfiguration="false" />
    <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" />
      <remove name="WebDAV" />
    </handlers>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
  </system.webServer>

数据控制网页API:

data controller web api:

public HttpResponseMessage Options()
{
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
}

public HttpResponseMessage PutOrganisation(int id, [FromBody]Organisation org)
{
  var opStatus = _Repository.UpdateOrganisation(org);
  if (opStatus.Status)
  {
     return Request.CreateResponse<Organisation>(HttpStatusCode.Accepted, org);
  }

return Request.CreateErrorResponse(HttpStatusCode.NotModified, opStatus.ExceptionMessage);
}

这是我的问题:为什么我得到ERRORMSG(见上图),当我做完全的小提琴手相同的请求(作品)作为angularclient(不工作)?

This is my question: Why do I get the errormsg ( see above) when I make exactly the same request in fiddler ( works) as in the angularclient (does not work)??

推荐答案

这是几乎可以肯定是CORS问题。我倒是第一次读了它,以确保你明白这是什么,以及为什么它是重要的。我猜想,您的服务器配置不正确。

This is almost certainly a CORS problem. I'd first read up about it to make sure you understand what this is and why it is important. And I would guess that your server configuration is not correct.

不幸的是,我不知道很多关于.NET,但这 CORS教程对于.NET 介绍你应该做的事情很清楚。

Unfortunately, I don't know much about .net, but this CORS tutorial for .net describes what you should do quite clearly.

它看起来像你缺少一个 EnableCors 注释。它看起来像你需要添加类似 [EnableCors(*,*,*)] 到控制器。选项​​的显式处理是没有必要的。当然,在producition,你不想使用通配符为您处理CORS。它们应该是更具体的,但是这是细用于测试

It looks like you are missing an EnableCors annotation. It looks like you need to add something like [EnableCors("*", "*", "*")] to your controller. Explicit handling of OPTIONS is not necessary. Of course, in producition, you don't want to use wildcards for your CORS handling. They should be more specific, but this is fine for testing.

这篇关于所请求的资源不支持HTTP方法“选项”。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆