405 方法不允许 Web API [英] 405 method not allowed Web API
本文介绍了405 方法不允许 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个错误很常见,我尝试了所有的解决方案,但没有一个奏效.我在控制面板中禁用了 WebDAV 发布并将其添加到我的 Web 配置文件中:
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;
}
这是抛出异常的地方:
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);
未放置.
您的 Web API 方法仅接受 PUT 请求.
Your Web API method accepts only PUT requests.
所以:
await client.PutAsJsonAsync("api/products", product);
这篇关于405 方法不允许 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文