Search code examples
jqueryasp.net-mvcasp.net-web-apiasp.net-mvc-5put

Web API Controller not found on PUT


I am having trouble finding the put action of a web api 2 controller. I'm using MVC5. The GET action in the controller is found and is working well when called. I have tried several different ways of hitting this, but to no avail. I would appreciate any hints.

The jquery call:

 var data = {
        id: 1,
        text: 'test'
    };

var jsondata = JSON.stringify(data);
$.ajax({
    url: serviceRoot  + "Approval/" +  id,
    type: 'PUT',
    contentType: "application/json; charset=utf-8",
    data: jsondata,
    success: function (results) {
        alert('Content saved.');
    }
})

The request object used in the controller:

public class ContentRequest
    {
        public int id { get; set; }
        public string text { get; set; }
    }

Route from startup:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

The controller:

using System.Collections.Generic;
using System.Web.Http;
using System.Net;
using System.Net.Http;

namespace SeacretGarden.Web.Controllers
{
    public class ApprovalController : ApiController
    {

        [HttpPut]
        public IHttpActionResult Put(int id, ContentRequest content)
        {
            return StatusCode(HttpStatusCode.NoContent);
        }
    }
}           

EDIT: The resolution to my problem was to add the following to my web.config file: ( I am using MVC5 with Web API controllers )

<modules>
      <remove name="FormsAuthentication" />
      <remove name="WebDAVModule"/>
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV"/>
      <add name="ExtensionlessUrlHandler-Integrated-4.0"
           path="*."
           verb="*"
           type="System.Web.Handlers.TransferRequestHandler"
           preCondition="integratedMode,runtimeVersionv4.0" />

    </handlers>

Solution

  • The resolution to my problem was to add the following to my web.config file: ( I am using MVC5 with Web API controllers ):

    <modules>
          <remove name="FormsAuthentication" />
          <remove name="WebDAVModule"/>
        </modules>
        <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <remove name="WebDAV"/>
          <add name="ExtensionlessUrlHandler-Integrated-4.0"
               path="*."
               verb="*"
               type="System.Web.Handlers.TransferRequestHandler"
               preCondition="integratedMode,runtimeVersionv4.0" />
    
        </handlers>