0

I've created a default Web Api 2 project in VS2013. My PUT action in my controller looks like this:

    // PUT: api/Events/5
    [HttpPut]
    [ResponseType(typeof(void))]
    public IHttpActionResult PutEvent(int id, Event @event)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != @event.Id)
        {
            return BadRequest();
        }

        db.Entry(@event).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!EventExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

My web.config looks like this:

<handlers>
      <remove name="WebDAV"/>
      <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>

And I have the following code in my WebConfig.cs

// Web API routes
config.MapHttpAttributeRoutes();

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

When I run the project and try to make a call (PUT with raw json data) to the api with Postman, the server returns with:

The requested resource does not support http method 'PUT'

I've looked at this post, but don't see what I'm doing different (Except that I added <remove name="WebDAV" />). But without it is also not working.

Community
  • 1
  • 1
kwv84
  • 943
  • 3
  • 11
  • 25

1 Answers1

0

Turned out that I had to change my PUT action and remove the id:

// PUT: api/Events/5
[HttpPut]
[ResponseType(typeof(void))]
public IHttpActionResult PutEvent(int id, Event @event)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    if (id != @event.Id)
    {
        return BadRequest();
    }

    db.Entry(@event).State = EntityState.Modified;

    try
    {
        db.SaveChanges();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!EventExists(id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }

    return StatusCode(HttpStatusCode.NoContent);
}
kwv84
  • 943
  • 3
  • 11
  • 25