简体   繁体   中英

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


    }
}

in web.config system.webServer:

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

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.

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