API Endpoint只允许GET进行POST

时间:2017-08-07 18:16:13

标签: asp.net-mvc reactjs asp.net-web-api

我正在使用使用ASP.NET MVC Framework构建的现有后端构建Web应用程序的React.js前端部分。 API控制器中的方法名为Post(应该将其默认为post方法),甚至还有[HttpPost]装饰器。我最近启用了CORS,但我不确定这是否重要。

API也有Swagger(如果你之前没有使用它就像postman)启用,当我从swagger调用方法作为POST时,它将工作并返回正确的数据。

但是,当我尝试从我的UI组件调用该方法时,API端点将提供405 Method Not Allowed状态代码。

它说该方法只允许GET,这是令人困惑的,因为它应该只允许POST并且它在swagger上工作或者如果我使用浏览器手动移动到URL。有没有人使用React组件或使用ASP.NET MVC / Web API 2有这样的问题?

我完全陷入困境......

编辑: 我正在为post端点添加API的方法。

[Route("{id}/merchant/SearchMerchants")]
[HttpPost]
[AuthorizeUserPermissionsToken(Cookie)]
public Response Post(Request rq)
{ // I put a breakpoint here to debug
    // some code
}

我不得不编写很多代码,但这里是post方法。

但我不认为它与API端点有关。 这是因为当我调试时,当我使用UI时,它甚至都不会到达断点或完全调用方法。但是,当我在浏览器上访问端点或使用招摇时,它将到达断点。

编辑2: 编辑3 :(删除编辑2并将原始标题移至底部以减少混淆)

行前OPTIONS个请求标题

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host: *redacted domain*
Origin:http://localhost:49702
Pragma:no-cache
Referer:http://localhost:49702/FindMerchants
User-Agent:Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Mobile Safari/537.36

OPTIONS请求的响应标头:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin:http://localhost:49702
Allow:OPTIONS, TRACE, GET, HEAD, POST
Content-Length:0
Date:Mon, 07 Aug 2017 18:41:42 GMT
Public:OPTIONS, TRACE, GET, HEAD, POST
Server:Microsoft-IIS/10.0
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN

然后是飞行前的真实POST请求标头:

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:154
Content-Type:application/json
Cookie: *redacted a bunch of cookies*
Host:a *redacted domain*
Origin:http://localhost:49702
Pragma:no-cache
Referer:http://localhost:49702/FindMerchants
User-Agent:Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Mobile Safari/537.36

真实的响应标题是这样的:

Access-Control-Allow-Credentials:true   
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept    
Access-Control-Allow-Origin:http://localhost:49702    
Allow:GET    
Cache-Control:no-cache    
Content-Length:73    
Content-Type:application/json; charset=utf-8    
Date:Mon, 07 Aug 2017 17:58:28 GMT    
Expires:-1    
Pragma:no-cache    
Server:Microsoft-IIS/10.0    
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN

编辑4:

Request URL:*redacted domain*/380/merchant/SearchMerchant
Request Method:POST
Status Code:405 Method Not Allowed
Remote Address:127.0.0.1:80
Referrer Policy:no-referrer-when-downgrade

编辑#9999(jk):

我的react组件只是调用我编写的自定义数据服务,它有一个像这样的post方法。

post(relativeUrl, data, apiVersion) {
    var url = this.getUrl(relativeUrl, apiVersion);
    var res = $.ajax({
        type: 'POST',
        url: url,
        contentType: 'application/json',    
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        data: JSON.stringify(data)
    });
    return res;
}

我只是传递relativeUrl来获取与用户帐户相关的列表。 在我进行失败的调用(显示405)之前,我使用相同的方法登录并更新应用程序的全局redux状态。更新全局状态后,我呈现一个不同的组件,并再次使用此方法与另一个relativeUrl来获取列表。然后我得到了错误。我还确保登录呼叫已完成,并在继续之前返回并更新状态。

1 个答案:

答案 0 :(得分:1)

要关注我的评论,

web.config文件中的

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, HEAD" />
    </customHeaders>
</httpProtocol>