Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 29-May-18 17:40pm

1 solution

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[^]
 
Share this answer
 
Comments
jim1972 30-May-18 0:10am    
Tried, but same effect.
Bryian Tan 30-May-18 0:49am    
for testing purposes, try it will AllowAnyHeader, AllowAnyMethod, AllowAnyOrigin and make sure to enable the policy in Configure method
jim1972 30-May-18 12:06pm    
Tried all these. but still the same.
Richard Deeming 31-May-18 14:12pm    
CORS only applies to requests from Javascript. Requests from C# will not be affected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900