【问题标题】:httpclient post async returning method not allowed (405)不允许 httpclient 发布异步返回方法 (405)
【发布时间】:2017-05-31 14:24:30
【问题描述】:

目前我在将一些数据发布到 web api 时遇到问题。我有两个不同的项目。一个项目是运行 web api 2.0 的 web 应用程序。另一个项目是一个简单的控制台应用程序,它创建一个新的 http 客户端,构建一个 json 对象,并尝试将其发布到 web api 控制器。但是,在发帖时我不断收到 405(不允许的方法...)。我已经在互联网上搜索了解决方案。我什至已经检查了 IIS Express 中的处理程序,并将其与我在 web.config 中的处理程序进行了比较。还是没有运气。

这就是我的 web api 的内容:

[RoutePrefix("api/media")]
[Authorize]
public class MultimediaController : ApiController
{
    [HttpPost, Route("test")]
    public IHttpActionResult Store([FromBody] TestRequest request) {
        return Ok(request);
    }
...

接下来,这是我的测试模型:

public class TestRequest {
    public string testProp1 { get; set; }
    public string testProp2 { get; set; }
}

然后,这是我的控制台应用程序中尝试执行 POST 的代码片段:

        private const string testUrl = "/api/media/test/";

        private async Task<bool> RunPostTest()
        {
            var httpClient = CreateHttpClient();
            var content = JsonConvert.SerializeObject(new TestRequest {testProp1 = "hello", testProp2 = "world"});
            var jsonContent = new StringContent(content, Encoding.UTF32, "application/json");

            var response = await httpClient.PostAsync(testUrl, jsonContent);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Response code: {response.StatusCode}");
                return false;
            }

            return true;
        }

这是我的处理程序的 web.config:

  <system.webServer>
    <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" />
    </handlers>

我检查了 iisexpress applicationhost.config。那里有很多处理程序,但我只会发布最后几个,因为那些看起来像是相关的。另请注意,WebDAV 到处都被注释掉了...

        ...
        <add name="TRACEVerbHandler" path="*" verb="TRACE" modules="ProtocolSupportModule" requireAccess="None" />
        <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" />
        <add name="ExtensionlessUrl-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
        <add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
        <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
    </handlers>
</system.webServer>

这会不断返回一个 http 405。我这里还有其他方法可以执行 GET,并且这些方法可以很好地到达终点。

另外值得注意的是,我使用了一个外部应用程序来执行一个帖子,例如 Postman,在正文中发送原始 json。并且终点被击中!似乎它与 iisexpress 的设置有关,但我可能是错的。

有人知道可能是什么情况吗?或者向我推荐一些可以发现问题的链接?

我使用即时窗口从调试中返回的响应对象:

response
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
  Cache-Control: no-cache
  Date: Mon, 16 Jan 2017 17:39:32 GMT
  Server: Microsoft-IIS/10.0
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 1218
  Allow: POST
  Content-Type: text/html
  Expires: -1
}}
    Content: {System.Net.Http.StreamContent}
    Headers: {Pragma: no-cache
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
Cache-Control: no-cache
Date: Mon, 16 Jan 2017 17:39:32 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
}
    IsSuccessStatusCode: false
    ReasonPhrase: "Method Not Allowed"
    RequestMessage: {Method: POST, RequestUri: 'http://localhost:45106/api/media/test/', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
  Accept: application/json
  Content-Type: application/json; charset=utf-32
  Content-Length: 164
}}
    StatusCode: MethodNotAllowed
    Version: {1.1}

【问题讨论】:

  • 能否发布错误详情?
  • 是的...我将添加来自即时窗口的响应对象结果

标签: c# json http iis asp.net-web-api


【解决方案1】:

确保您在 API 项目的 Web.config 文件的 &lt;system.webServer&gt; 部分中有以下内容:

<modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/> <!-- add this -->
</modules>

此外,在处理程序部分:

<handlers>
    <remove name="WebDAV" />
    ...
</handlers>

【讨论】:

  • 好的,我添加了这些,但仍然收到相同的 405 错误
【解决方案2】:

在下面试试这个:添加 ExtensionlessUrlHandler* 并将其添加回来。它对我有用。

  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule" />
  </modules>
    <handlers>
      <remove name="WebDAV" />
      <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>

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 2018-03-18
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多