Dear friends,
First of all, I'm sorry if this question has already been solved and I could not find it in the forums.
I'm having a strange problem in my WebApi2. I have a fully functioning Get, Post and Put methods. However, I don't know why the Delete method is giving 405 - Method Not Allowed error.
The methods have the same built structure and authorization, so I believe this is due to a IIS configuration situation.
Here is the error:
Here is the WebApi2 code:
[HttpDelete] [Route("")] public bool Delete(string userId) { ThreeLayerDemo.Logic.BUS.UserBUS objOperations = new ThreeLayerDemo.Logic.BUS.UserBUS(); bool deleteResult = objOperations.Delete(userId); return deleteResult; }
Here is the client consuming code:
DialogResult dR = MessageBox.Show("Delete?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (dR == System.Windows.Forms.DialogResult.Yes) { string idCliente = gridClientes.Rows[gridClientes.SelectedRows[0].Index].Cells[0].Value.ToString(); HttpClientHandler handler = new HttpClientHandler(); handler.Credentials = new NetworkCredential("tiago", "???"); using (var client = new HttpClient(handler)) { client.Timeout = TimeSpan.FromSeconds(500); var jsonVariable = new JavaScriptSerializer().Serialize(idCliente); System.Threading.Tasks.Task<HttpResponseMessage> responseTask; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Delete, "http://dataServices.????.com/api/clientes"); //req.Content = new StringContent(jsonVariable, Encoding.UTF8, "application/json"); req.Content = new StringContent(idCliente); responseTask = client.SendAsync(req); responseTask.Wait(); MessageBox.Show(responseTask.Result.ToString()); } }
Thank you for your help in advance.