1

I started a new Web Api MVC4 project and am having no luck figuring out what I am doing wrong with the routers. My routes for simple Get requests are returning 404.

Here are my route definitions:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("ApiDefault", "api/{controller}/{id}",
                        new {controller = "Home", id = UrlParameter.Optional});

        routes.MapRoute("ApiDefault-CategoryLevels", "api/{controller}/{level1}/{level2}/{level3}/{level4}",
                        new
                            {
                                controller = "Home",
                                level1 = UrlParameter.Optional,
                                level2 = UrlParameter.Optional,
                                level3 = UrlParameter.Optional,
                                level4 = UrlParameter.Optional
                            });

        routes.MapRoute("Default", "{controller}/{action}/{id}",
                        new {controller = "Home", action = "Index", id = UrlParameter.Optional});
    }

I am trying to get the ApiDefault-CategoryLevels route to be used. I installed Phil Haack's routing debugger and it is showing that when I browse to this Url:

http://localhost:22283/api/Categories/a/b/c/d

that the route I am looking for IS matching:

Matched Route: api/{controller}/{level1}/{level2}/{level3}/{level4}

Route       Data
Key         Value
controller  Categories 
level1      a 
level2      b 
level3      c 
level4      d

However the return from the IIS Webserver is a 404.

When I call the same URL in this manner, using a querystring, it works:

http://localhost:22283/api/Categories?level1=A&level2=B&level3=C&level4=D

The web request works and I get back the results I was expecting. It is only when I try the request the first way does it return a 404.

Here is the entirety of my CategoriesController:

public class CategoriesController : ApiController
{

    public IEnumerable<CategoryModel> Get(string level1 = null, string level2 = null, string level3 = null, string level4 = null)
    {
        return new[] {new CategoryModel()};
    }

    public CategoryModel Get(Guid id)
    {
        return new CategoryModel();
    }
}
2
  • It matches action "a", which is not present in your controller. Commented Jan 30, 2013 at 18:30
  • I fixed the code above. Sorry about that. I had a stale window I was copy and pasting from (I had tried a bunch of different things.) The code above should reflect the issue accurately now. (no action at all, it is an ApiController) Commented Jan 30, 2013 at 18:37

1 Answer 1

3

You are adding your ApiController-based routes to RouteTable.Routes. You should be adding them to GlobalConfiguration.Configuration. That is, it appears you have added them to the MVC 4 project template's RouteConfig.RegisterRoutes(RouteCollection). If you use the MVC4 Web API project template, add them to the WebApiConfig.Register(HttpConfiguration) method, instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I didn't even notice that was there! Thanks so much.

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.