Multiple url levels in .NET Web API

.net, asp.net-web-api, routes

Solution

The routing is almost correct but the main problem is that you are missing the required HttpMethod Attributes on the other action methods. `[HttpGet]` is inferred on the first method because of its name. This is what you need:

public class FormController : ApiController
{
    // api/form
    public string Get()
    {
        return "OK-Get";
    }

    // api/form/method1
    [HttpGet]
    public string Method1()
    {
        return "OK1";
    }

    // api/form/method2
    [HttpGet]
    public string Method2()
    {
        return "OK2";
    }
}

With a route map that belongs in App_Start/RouteConfig.cs

routes.MapHttpRoute(
    name: "FormApi",
    routeTemplate: "api/form/{action}",
    defaults: new { controller = "form", action = "Get"}
);

For more information read http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection

Problem

I'm setting up a rest service using the new .NET Web API, and I've come across a little issue. We want to have a little different routing, but I'm not sure how to achieve this. ``` public class FormController : ApiController { // api/form public string Get() { return "OK-Get"; } // api/form/method1 public string Method1() { return "OK1"; } // api/form/method2 public string Method2() { return "OK2"; } } ``` But this doesn't work. If I go to `/api/form/method2`, I get `OK-Get` as a response. I'm thinking this has to do with routing, but I'm not sure, since I haven't used MVC before. I've tried setting it up like this in the `WebApiConfig.cs`: ``` config.Routes.MapHttpRoute( name: "FormApi", routeTemplate: "api/form/{action}" ); ``` But that did nothing.

Original source