'Error: System.Net.Http.HttpRequestException: Response status code does not indicate success: 405 (Method Not Allowed)

I am getting following exception when calling an Asp.NET Core 3.1 web api from a Blazor app. But same code works great from visual studio debugging

Response status code does not indicate success: 405 (Method Not Allowed).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at Microsoft.AspNetCore.Components.HttpClientJsonExtensions.SendJsonAsync[T](HttpClient httpClient, HttpMethod method, String requestUri, Object content)*

UI Code:

public async Task<bool> UpdateCOAValue(COALookUps dataItem)
{
    bool result = false;
    
    try
    {
        bool response = await _httpClient.SendJsonAsync<bool>(HttpMethod.Put, string.Format(@_webApi.WebAPIUrl, "update"), dataItem);
        result = await Task.FromResult(response);
    }
    catch (Exception ex)
    {
        Log.Error("Error: {0}", ex);
    }

    return result;
}

Web API Controller Method:

[HttpPut("update")]
public bool UpdateCOAEntry([FromBody]COALookups value)
{
    try
    {
        List<SqlParameter> lstSQLParams = new List<SqlParameter>();
        SqlParameter paramCOALookUpID = new SqlParameter();
        //other code
        dbManager.Update("UpdateCOALookUp", CommandType.StoredProcedure, lstSQLParams.ToArray());
    }
    catch (Exception ex)
    {
        Log.Error("Error: {0}", ex);
        return false;
    }
    
    return true;
}

Web API Controllers syntax:

[Route("api/[controller]")]
[ApiController]
public class COAController : ControllerBase
{
}


Solution 1:[1]

Here is what worked for me. (this is a workaround), will have to redo this after each release. Please post if anyone has a better solution.

  1. Open WebDav Authoring Rules and then select Disable WebDAV option present on the right bar.

  2. Select Modules, find the WebDAV Module and remove it.

  3. Select HandlerMapping, find the WebDAVHandler and remove it.

Solution 2:[2]

I found this solution working than changing any settings in IIS In ConfigureServices method add following

var handler = new HttpClientHandler()
{
    UseDefaultCredentials = false,
    Credentials = System.Net.CredentialCache.DefaultCredentials,
    AllowAutoRedirect = true
};
    
services.AddSingleton(sp =>
    new HttpClient(handler)
    {
        BaseAddress = new Uri(Configuration["WebAPI:BaseUrl"])
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2832577
Solution 2 FranzHuber23