2

I am working with an ASP.Net Web Api project on Web Developer Express 2010. The routing config is defined in WebApiConfig.cs as:

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

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

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

An API Controller called "GCURObservationController" has an action as:

[HttpGet, ActionName("retrieveCuringMaps")]
    public IList<SimpleCuringMapsModel> retrieveCuringMaps()
    {
        ... ...
        return jsonCuringMapModels;
    }

The project was compiled and run successfully. However, I had to go to

http://localhost:2061/api/GCURObservation/retrieveCuringMaps/0

to get the action triggered (action name followed by any integer), rather than what I expected to be

http://localhost:2061/api/GCURObservation/retrieveCuringMaps

That means an arbitrary integer had to follow the action name to get it right. Otherwise, the error was returned. I don't want this action to be triggered with any param.

{"Message":"The request is invalid."}

How to get the second URL to work? Thanks

Cheers, Alex

4
  • 1
    Routing works via fall through (think of it like case where it falls till it matches), so the order of the routes matters a lot. I think you can fix this by swapping DefaultApi3 and DefaultApi4, or by changing your action to retrieveCuringMaps(int id = 0) Commented Jan 6, 2014 at 0:44
  • Thanks Siva. However, it still gave the same error after I swapped DefaultApi3 and DefaultApi4. Commented Jan 6, 2014 at 1:21
  • 1
    Just out of curiosity what happens when you remove the DefaultApi route? That seems fundamentally flawed as you can't have a route without an Action. Commented Jan 6, 2014 at 2:41
  • Yes, I removed the DefaultApi and the it worked. The DefaultApi was automatically created when the project was created in Visual Web Developer. I think it is equivalent to /api/{controller}/get/{id}. I am still curious about why they conflict with each other. Commented Jan 6, 2014 at 3:32

1 Answer 1

3

If you are using Web API 2, following is one solution you could use. In the below example, I am using attribute routing and conventional routing together in one controller. Here all the actions except GetCustomerOrders are reached via conventional route "DefaultApi".

In general the idea here is not new, that is...even without Web API 2's attribute routing, you could define routes for each individual action of a controller in the global route table, but attribute routing makes this process easier as you can define routes directly and near to the action.

config.MapHttpAttributeRoutes();

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

[RoutePrefix("api/customers")]
public class CustomersController : ApiController
{
    public IEnumerable<Customer> GetAll()
    {
    }

    public Customer GetSingle(int id)
    {
    }

    public void Post(Customer customer)
    {
    }

    public void Put(int id, Customer updatedCustomer)
    {
    }

    public void Delete(int id)
    {
    }

    [Route("{id}/orders")]
    public IEnumerable<Order> GetCustomerOrders(int id)
    {
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.