locked
PUT method not supported RRS feed

  • Question

  • User-400158872 posted

    I am trying to test my Rest Web API through postman. I got the error 

     "Message": "The requested resource does not support http method 'PUT'."

    [HttpPatch]
            [HttpPut]
            public HttpResponseMessage PUT(int id,Contact contact)
            {
                contact.ID = id;
                if(!contactRepository.Update(contact))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                return Request.CreateResponse(HttpStatusCode.OK);
            }

    I have define Put method in my controller. Why am I getting this error?

    "Message": "The requested resource does not support http method 'PUT'."

    Thursday, August 27, 2015 11:40 AM

Answers

  • User281315223 posted

    Can you check to ensure that the HttpPut attribute that you are referencing is coming from System.Web.Http or you can consider using a workaround like the one mentioned here which involves using the [PUT] attribute along with the route :

    [PUT("some/url"), HttpPut]
    public string Method2() {}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2015 1:11 PM
  • User-782957977 posted

    Your controller action method expects parameter id in the url. Also it is good practice to  add [FromBody] for contact parameter like this

    [HttpPatch]
            [HttpPut]
            public HttpResponseMessage PUT(int id,[FromBody]Contact contact)
            {
                contact.ID = id;
                if(!contactRepository.Update(contact))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                return Request.CreateResponse(HttpStatusCode.OK);
            }

    Now try to access Web Api with following code

    using (var client = new HttpClient())
                 {
                     // Create object of Contact and set properties
                     Contact contact = new Contact () ; 
                     client.BaseAddress = new Uri("http://localhost:58745/");
                     var response = client.PutAsJsonAsync("api/ControllerName/10", contact).Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2015 9:56 PM

All replies

  • User281315223 posted

    Can you check to ensure that the HttpPut attribute that you are referencing is coming from System.Web.Http or you can consider using a workaround like the one mentioned here which involves using the [PUT] attribute along with the route :

    [PUT("some/url"), HttpPut]
    public string Method2() {}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2015 1:11 PM
  • User-400158872 posted

    It gave me this  error.

    Error 2 'ContactManager.Controllers.ContactController.PUT(int, ContactManager.Models.Contact)' is a 'method' but is used like a 'type' C:\Users\stula1289\Documents\Visual Studio 2013\Projects\ContactManager\ContactManager\Controllers\ContactController.cs 72 10 ContactManager

    Thursday, August 27, 2015 4:28 PM
  • User-84896714 posted

    Hi sumittuladhar,

    I have define Put method in my controller. Why am I getting this error?

    Did you register route when application start?

    config.MapHttpAttributeRoutes();
    
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    Best Regards,
    Wang Li

    Friday, August 28, 2015 1:42 AM
  • User-400158872 posted

    Yes I did. All the other method like get,post,delete are working except for PUT method. I don't know whats wrong there.

    Friday, August 28, 2015 10:52 AM
  • User-782957977 posted

    Your controller action method expects parameter id in the url. Also it is good practice to  add [FromBody] for contact parameter like this

    [HttpPatch]
            [HttpPut]
            public HttpResponseMessage PUT(int id,[FromBody]Contact contact)
            {
                contact.ID = id;
                if(!contactRepository.Update(contact))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
    
                return Request.CreateResponse(HttpStatusCode.OK);
            }

    Now try to access Web Api with following code

    using (var client = new HttpClient())
                 {
                     // Create object of Contact and set properties
                     Contact contact = new Contact () ; 
                     client.BaseAddress = new Uri("http://localhost:58745/");
                     var response = client.PutAsJsonAsync("api/ControllerName/10", contact).Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2015 9:56 PM