.Net Web API Doesn't Match Route

Viewed 738

I am having some trouble understanding how Web API 2 handles routing.

  • I have created a PostsController that works just fine in terms of the standard actions, GET, POST, etc.
  • I tried to add a custom route that is a PUT action called Save() that takes a model as an argument.
  • I added [HttpPut] and [Route("save")] in front of the custom route. I also modified the WebAPIConfig.cs to handle the pattern api/{controller}/{id}/{action}
  • However, if I go to http://localhost:58385/api/posts/2/save in Postman (with PUT) I get an error message along the lines of No action was found on the controller 'Posts' that matches the name 'save'. Essentially a glorified 404.
  • If i change the route to be [Route("{id}/save")] the resulting error remains.

What am I doing incorrectly?

WebAPIConfig.cs

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

PostController.cs

// GET: api/Posts
public IHttpActionResult Get()
{
    PostsStore store = new PostsStore();
    var AsyncResult = store.GetPosts();
    return Ok(AsyncResult);
}

// GET: api/Posts/5
public IHttpActionResult Get(string slug)
{
    PostsStore store = new PostsStore();
    var AsyncResult = store.GetBySlug(slug);
    return Ok(AsyncResult);
}

// POST: api/Posts
public IHttpActionResult Post(Post post)
{
    PostsStore store = new PostsStore();
    ResponseResult AsyncResult = store.Create(post);
    return Ok(AsyncResult);
}

    // PUT: api/Posts/5 DELETED to make sure I wasn't hitting some sort of precedent issue.
    //public IHttpActionResult Put(Post post)
   // {

       //     return Ok();
    //}

[HttpPut]
[Route("save")]
public IHttpActionResult Save(Post post)
{
    PostsStore store = new PostsStore();
    ResponseResult AsyncResponse = store.Save(post);
    return Ok(AsyncResponse); 
}
1 Answers
Related