简体   繁体   中英

ASP.NET MVC API PUT getting 404 error

I have a .NET MVC RESTful API that works fine for GET and POST, but returns 404 for PUT requests:

[Authorize]
public class TasksController : ApiController
{
    // GET api/tasks
    /// <summary>
    /// Get all users tasks.
    /// </summary>
    /// <returns>Task Object (JSON serialised)</returns>
    public IEnumerable<Task> Get()
    {
        List<Task> tasks = new List<Task>();

        ...

        return tasks;

    }

    // GET api/tasks/5
    public Task Get(Int64 id)
    {
        Task thisTask = new Task();

        ...

        return thisTask;
    }

    // POST api/tasks
    public void Post(Task item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }

        ...
    }

    // PUT api/tasks/5
    public void Put(Int64 id, Task item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        } 

        ...
    }

    // DELETE api/tasks/5
    public void Delete(int id)
    {
        ...
    }

    // PUT, GET, POST, DELETE api/tasks...
    [AllowAnonymous]
    public HttpResponseMessage Options()
    {
        var response = new HttpResponseMessage();
        response.StatusCode = HttpStatusCode.OK;
        return response;
    }

}

Any idea why it wouldn't be picking up the PUT? (Even OPTIONS works fine)

Routing:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        // CORS Enabled
        //var cors = new EnableCorsAttribute("localhost", "*", "*");
        //config.EnableCors(cors);

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

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

Web.Config:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    <add name="Access-Control-Allow-Headers" value="Authorization, Content-Type" />
  </customHeaders>
</httpProtocol>

Addendum It looks like the speed at which it is coming back is instant, even after a recompile, So I'm guessing it's not even getting to the application, so must be a configuration issue.

Ok. So turns out it was due to having this commented out:

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

...in the web.config, which I originally did to be able to use shared hosting. Do'h!

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