0

I have designed Web API and Xamarin forms application. I am able to perform get and post operations but not able to perform put or delete operations through web API. I am getting below message

StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 07 Sep 2017 11:08:56 GMT
Allow: GET, HEAD, OPTIONS, TRACE
Content-Type: text/html
Content-Length: 1096
}

My code is as below

client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");


public async Task EditTodoItemAsync(hospitalModel item)
{
    string RestUrl = "http://www.helpinggrow.in/api/Hospital/{0}";
    var uri = new Uri(string.Format(RestUrl, item.hospitalId));

    try
    {
        var json = JsonConvert.SerializeObject(item);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        HttpResponseMessage response = null;

        response = await client.PutAsync(uri, content);

        if (response.IsSuccessStatusCode)
        {
            Debug.WriteLine(@"TodoItem successfully saved.");
        }

    }
    catch (Exception ex)
    {
        Debug.WriteLine(@"ERROR {0}", ex.Message);
    }
}
juunas
  • 54,244
  • 13
  • 113
  • 149
Akash Rekalwar
  • 85
  • 1
  • 1
  • 9
  • The route/method your trying to connect to in your WebAPI would have to allow Delete/Put operations. It'd be useful if you could add the relevant code relating to your web service above. Also look at your error message, it clearly tells you what is and isn't allowed: (Allow: GET, HEAD, OPTIONS, TRACE) – JoeTomks Sep 07 '17 at 12:28
  • 1
    Possible duplicate of [ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8](https://stackoverflow.com/questions/10906411/asp-net-web-api-put-delete-verbs-not-allowed-iis-8) – mjwills Sep 07 '17 at 12:29

1 Answers1

0

Asp.net disabled put and delete by default. You need to enable these in web.config. Sorry i'm not able to give you the exact config line as i'm on mobile at the moment

Tien Dinh
  • 1,037
  • 7
  • 12