无法执行从xamarin表单应用程序到asp.net web api的put,delete操作

时间:2017-09-07 12:23:52

标签: c# asp.net xamarin asp.net-web-api xamarin.ios

我设计了Web API和Xamarin表单应用程序。我能够执行get和post操作,但无法通过Web API执行put或delete操作。我收到消息

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
}

我的代码如下

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

1 个答案:

答案 0 :(得分:0)

Asp.net默认禁用put和delete。您需要在web.config中启用它们。抱歉,我暂时无法为您提供确切的配置行