Sanju Rao
Sanju Rao

Reputation: 331

WEB API PUT method is not working and getting HTTP/1.1 405 Method Not Allowed

Hi I am not understanding how to fix this can any one please let me know why my put method is not getting executed. I get error

HTTP/1.1 405 Method Not Allowed

in my PUT method. Is there any way to fix using ROUTE attribute to debug my PUT method.

public class UserProfessionController : ApiController
{
    //[Route("")]
    public IHttpActionResult Get()
    {
        return Ok();
    }

    [HttpPut]        
    public HttpResponseMessage UpdateUserProfession([FromUri]string categoryCode)
    {

        try
        {
            int userId = 1;
            if (String.IsNullOrWhiteSpace(categoryCode))
            {
                var specialitiesRepository = new UserRepository();
                if(specialitiesRepository.UpdateUserProfession(userId, categoryCode) > 0)
                {
                    return Request.CreateResponse(HttpStatusCode.Created);
                }

                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Invalid Input Data");
            }
            else
            {
                throw new Exception("Category Code-" + categoryCode + "For the User " + userId + " is not valid");
            }

        }
        catch (Exception ex)
        {
            var msg = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(ex.Message),
                ReasonPhrase = "Invalid Input Data"
            };
            throw new HttpResponseException(msg);
        }


    }
}

Upvotes: 0

Views: 1892

Answers (2)

Okundu Omeni
Okundu Omeni

Reputation: 1

After struggling with this issue - tried the changes to config and IIS regarding removing WebDAV with no success, I tried the following and the issue was resolved.

Remove WebDAV publishing from the IIS install from control panel/program and features - select "turn windows features on/off"

Found this solution at the link below.

https://ignas.me/tech/405-method-not-allowed-iis/

This fixed the issue on both Windows 10 and 7.

Upvotes: 0

aleha_84
aleha_84

Reputation: 8541

in web.config system.webServer:

<modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/> 
</modules>

Upvotes: 1

Related Questions