405方法不被允许网页API [英] 405 method not allowed web api
本文介绍了405方法不被允许网页API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此错误是很常见的,我尝试了所有的解决方案,其中非工作。我在控制面板中禁用WebDAV发布,并添加这我的网络配置文件:
This error is very common, and I tried all of the solutions and non of them worked. I have disabled WebDAV publishing in control panel and added this to my web config file:
<handlers>
<remove name="WebDAV"/>
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
错误仍然存在。这是控制器:
The error still persists. This is the controller:
static readonly IProductRepository repository = new ProductRepository();
public Product Put(Product p)
{
return repository.Add(p);
}
方法实现:
public Product Add(Product item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
item.Id = _nextId++;
products.Add(item);
return item;
}
而这正是抛出异常:
And this is where the exception is thrown:
client.BaseAddress = new Uri("http://localhost:5106/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsJsonAsync("api/products", product);//405 exception
有什么建议?
推荐答案
您是从客户张贴:
await client.PostAsJsonAsync("api/products", product);
不是思想促进。
not PUTing.
您的Web API方法只接受PUT请求。
Your Web API method accepts only PUT requests.
所以:
await client.PutAsJsonAsync("api/products", product);
这篇关于405方法不被允许网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文