简体   繁体   中英

Not able to perform put,delete operation from xamarin forms application to asp.net web api

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);
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM