Http动词 - PUT,PATCH,DELETE无法从客户端工作 [英] Http verbs - PUT, PATCH, DELETE not working from client
问题描述
您好,
我创建了一个ASP.Net Core 2.0 API项目。
当我从邮递员运行api时,它运行正常。
但是当我试图从WinForm应用程序运行时,它只适用于Get和Post操作。 DELETE,PUT和PATCH无法正常工作。错误来自'方法不允许'。
我用Google搜索并试图在IIS的RequestFiltering中添加这些动词。但它仍然存在。
我在本地机器上运行它。有人可以知道如何解决这个问题,赞赏。
这是我的代码片段: -
private void cmdDelete_Click(object sender,EventArgs e)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(http://www.test.com/);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(Application / json));
var uri =api / customers /+ txtId.Text.Trim();
HttpResponseMessage response = client.DeleteAsync(uri).Result;
if(response.IsSuccessStatusCode)
{
MessageBox.Show(Success);
}
else
MessageBox.Show(Error:+ response.StatusCode +:+ response.ReasonPhrase);
}
public async任务测试()
{
客户cust = new Customer();
int custId = Convert.ToInt32(txtId.Text);
await cust.DeleteCustomerDetails(custId);
}
注意:请注意邮政编码的API工作正常。
问候
吉姆
我尝试过:
我用Google搜索并在IIS中尝试将httpverbs正确添加到RequestFiltering中。但仅此一点无效。
您可能需要在应用程序中启用启用跨源请求(CORS)
在ASP中启用跨源请求(CORS) .NET Core | Microsoft Docs [ ^ ]
Hello,
I have created a ASP.Net Core 2.0 API project.
When i run the api from postman , it works fine.
But when i tried to run from a WinForm application, it works only the Get and Post operations. DELETE, PUT and PATCH not working. Error comes is 'Method Not Allowed'.
I googled and tried to add these verbs in IIS's RequestFiltering. But still it persists.
Am running this in local machine. Can someone know how to resolve this, appreciated.
Here is my code snippet:-
private void cmdDelete_Click(object sender, EventArgs e) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://www.test.com/"); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("Application/json")); var uri = "api/customers/" + txtId.Text.Trim(); HttpResponseMessage response = client.DeleteAsync(uri).Result; if (response.IsSuccessStatusCode) { MessageBox.Show ("Success"); } else MessageBox.Show ("Error : " + response.StatusCode + " : " + response.ReasonPhrase); } public async Task test() { Customer cust = new Customer(); int custId = Convert.ToInt32(txtId.Text); await cust.DeleteCustomerDetails(custId); }
NB : please note the API is working fine from Postman.
Regards
Jim
What I have tried:
I have googled and tried in IIS duly adding the httpverbs into the RequestFiltering. But that alone didnt work.
You might need to enable the Enable Cross-Origin Requests (CORS) in the application
Enable Cross-Origin Requests (CORS) in ASP.NET Core | Microsoft Docs[^]
这篇关于Http动词 - PUT,PATCH,DELETE无法从客户端工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!