C# 不允许httpclient后异步返回方法(405)
目前,我在向web api发布一些数据时遇到了问题。我有两个不同的项目。一个项目是运行WebAPI2.0的web应用程序。另一个项目是一个简单的控制台应用程序,它创建一个新的http客户端,构建一个json对象,并尝试将其发布到web api控制器。然而,做文章我一直得到405(方法不允许…)。我在互联网上搜寻解决方案。我甚至还检查了IIS Express中的处理程序,并将其与web.config中的处理程序进行了比较。还是不走运 下面是我的web api的内容:C# 不允许httpclient后异步返回方法(405),c#,json,http,iis,asp.net-web-api,C#,Json,Http,Iis,Asp.net Web Api,目前,我在向web api发布一些数据时遇到了问题。我有两个不同的项目。一个项目是运行WebAPI2.0的web应用程序。另一个项目是一个简单的控制台应用程序,它创建一个新的http客户端,构建一个json对象,并尝试将其发布到web api控制器。然而,做文章我一直得到405(方法不允许…)。我在互联网上搜寻解决方案。我甚至还检查了IIS Express中的处理程序,并将其与web.config中的处理程序进行了比较。还是不走运 下面是我的web api的内容: [RoutePrefix("a
[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; }
}
然后,下面是我的控制台应用程序中的一段代码,它试图完成这篇文章:
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;
}
确保在API项目的
Web.config
文件的
部分中包含以下内容:
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- add this -->
</modules>
此外,在处理程序部分:
<handlers>
<remove name="WebDAV" />
...
</handlers>
...
尝试以下操作:添加无扩展UrlHandler*
并将其重新添加。它对我有用
<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>
您可以发布错误详细信息吗?可以。。。我将从即时窗口添加响应对象结果确定我添加了这些结果,但仍然收到相同的405错误
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- add this -->
</modules>
<handlers>
<remove name="WebDAV" />
...
</handlers>
<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>