10

I've been trying to add a second POST method to the default ValuesController class that will take an id parameter and act identical to the PUT method, like so:

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

namespace WebCalendar.Controllers {
    public class ValuesController : ApiController   {
        // GET /values
        public IEnumerable<string> Get()             {
            return new string[] { "value1", "value2" };
        }

        // GET /values/5
        public string Get(int id) {
            return "value";
        }

        // POST /values
        public void Post(string value) {
        }

        // POST /values/5
        public void Post(int id, string value) {
            Put(id, value);
        }

        // PUT /values/5
        public void Put(int id, string value){
        }

        // DELETE /values/5
        public void Delete(int id) {
        }
    }
}

Problem is, when I add this second post method, any time I make a POST request, I get the error:

"No action was found on the controller 'values' that matches the request."

If I comment out one of the methods (doesn't matter which one), POST will work with the other method. I've tried renaming the methods, and even using [HttpPost] on both of them, but nothing has worked.

How can I have more than one POST method in a single ApiController?

EDIT

Here is the only route that I'm using:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new { controller = "values", id = RouteParameter.Optional }
);
4
  • 2
    Can you please post your routes? Commented Apr 6, 2012 at 3:51
  • No limit for how many post methods you can have on the controller. I suspect it's a routing issue Commented Apr 6, 2012 at 3:54
  • What's the URL you're calling to make this fail? What happens if you provide a query string for value? I think the issue is the value parameter has to be there for the routing to work since it's not marked optional. Commented May 10, 2012 at 1:02
  • Have added a few point here that worked for me: stackoverflow.com/questions/14325794/… Commented Mar 18, 2014 at 18:49

1 Answer 1

9

You have to include the action in your route:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Sign up to request clarification or add additional context in comments.

4 Comments

Marking this as an answer (because it works), but what I don't understand is: why is the action required? The two separate Get methods work without it, so why do I need to add it for the Post methods?
If you're removing the value param from the first POST it would work without specifying the action explicitly, but you wouldn't be able to post any data. You can handle posts only using unique URIs.
It works with Get because the {id} is an optional route parameter. So values/get routes to Get() and values/get/1 routes to Get(int). You really need to define manually the route.
Put this in WebApiConfig.cs, not RouteConfig.cs :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.