.Net Core 2.2 Web API 405 [英] .Net Core 2.2 Web API 405

查看:421
本文介绍了.Net Core 2.2 Web API 405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置.net core 2.2 Web API以使用后动词。无论get谓词是在我的本地计算机(w10 iis eXPRESS 10.0)还是Windows服务器(2016 R2 IIS 8.0)上运行,除get谓词外,其他都将返回405。我已经阅读了其他有关在配置文件中禁用WebDav,添加路由以及完全删除WebDav功能的文章。我没有做所有这些。我刚刚开始在核心领域进行开发并发现令人困惑,在同一台服务器上是运行在.net framework 4.5上的非核心网络api,该API可以正确处理GET,PUT,POST,DELETE。是的,我在对任何配置进行更改后都重新启动了服务器。以下是我所做的web.config更改,最后一个直接来自MS。






详细说明



不是问题。



您向 https:// localhost发出 GET 请求:44327 / api / values / 。它返回 200 OK



但是当您进行 POST 请求相同的URL https:// localhost:44327 / api / values / 。它说 405不允许的方法



但是,您会得到 405 。之所以发生这种情况,是因为您正在使用 POST 方法访问 GET 端点。


Microsoft Docs说:



... HTTP客户端向Web API应用程序的URL发送了有效的JSON请求在Web服务器上,但服务器返回了HTTP 405错误消息,指示该URL不允许使用PUT方法。相反,如果请求URI与Web API应用程序的路由不匹配,则服务器将返回HTTP 404 Not Found错误。



https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications


如果您只是删除 GET 端点。 POST 请求将开始返回 404 Not found



要发送 POST 请求,您需要使用其他URL

  [Route( api / [controller])] 
[ApiController]
public class ValuesController:ControllerBase
{

// POST api / values
[HttpPost( {val})]
public StatusCodeResult Post()
{
返回Ok();
}
}

此基于属性的配置表示您 POST 端点为 / api / Values / {val} 。其中 {val} 是任何值。



如果要处理它,则应将其传递给方法:

  [HttpPost( {val})] 
public StatusCodeResult Post(string val)
{
return Ok();
}


I am trying to setup a .net core 2.2 web api to use a post verb. Anything other than a get verb returns a 405 no matter if it is run on my local machine (w10 iis eXPRESS 10.0) or the windows server (2016 R2 IIS 8.0). I've read the other posts about disabling WebDav in your config file, adding a route, and completely removing the WebDav feature. I have done all of those to no avail. I'm just starting to develop in core and find this baffling, on the same server is a non-core web api that runs on .net framework 4.5 that processes GET,PUT,POST,DELETE without error. And yes, I have restarted the server after making changes to any of the configurations. The following are the web.config changes that I made, the last one coming directly from MS. Basic project that reproduces the same error on my machine and server is here https://github.com/FranciscanMedia/error405_core/tree/master it is just a standard web api project you get when you fire up VS2019.

<system.webServer>    
    <handlers accessPolicy="Read, Script">
       <remove name="WebDAV" />
       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
       <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"
          path="*."
          verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
          modules="IsapiModule"
          scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
          preCondition="classicMode,runtimeVersionv4.0,bitness64"
          responseBufferLimit="0" />
    </handlers>
</system.webServer>

<system.webServer>    
    <modules>        
        <remove name="WebDAVModule" />    
    </modules>    
    <handlers>        
        <remove name="WebDAV" />    
    </handlers>
</system.webServer>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="false">
        <remove name="WebDAVModule"/>
    </modules>    
</system.webServer>

<system.webServer>
    <handlers accessPolicy="Read, Script">
        <remove name="WebDAV" />
        <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
        <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"
           path="*."
           verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
           modules="IsapiModule"
           scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
           preCondition="classicMode,runtimeVersionv4.0,bitness64"
           responseBufferLimit="0" />
    </handlers>
</system.webServer>

解决方案

Short answer

It could be as simple as that. The reason is routing.

Just send your POST request to right URL like https://localhost:44327/api/values/123.


Detailed explanation

It's not the issue. It works as expected.

You make a GET request to https://localhost:44327/api/values/. It returns 200 OK.

But when you make a POST request to the same URL https://localhost:44327/api/values/. It says 405 Method not allowed.

However, you get 405. It is happening because you are hitting the GET endpoint with POST method.

Microsoft Docs says:

... the HTTP client sent a valid JSON request to the URL for a Web API application on a web server, but the server returned an HTTP 405 error message which indicates that the PUT method was not allowed for the URL. In contrast, if the request URI did not match a route for the Web API application, the server would return an HTTP 404 Not Found error.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

If you simply remove the GET endpoint. The POST request will start returning 404 Not found. Which means that you are not hitting any registered route.

To send POST request you need to use different URL according to the routing rules.

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{

    // POST api/values
    [HttpPost("{val}")]
    public StatusCodeResult Post()
    {
        return Ok();
    }
}

This attribute-based configuration means that route of your POST endpoint is /api/Values/{val}. Where {val} is any value. It's not processed in the endpoint.

If you want to process it, you should pass it to the method:

[HttpPost("{val}")]
public StatusCodeResult Post(string val)
{
    return Ok();
}

这篇关于.Net Core 2.2 Web API 405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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