Question

I've been struggling with this problem for at least 4 hours now, trying every solution I could find.

My main project is set up with Asp.Net and MVC which works great routing to controllers within the project. I'm integrating a library that exposes a JSON api that uses ApiControllers in another project but I can't get the main project to route to anything in the library project, it just returns a strait 404.

In my main project global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{*allaxd}", new { allaxd = @".*\.axd(/.*)?" });
        routes.IgnoreRoute("{*less}", new { less = @".*\.less(/.*)?" });

        //ignore aspx pages (web forms take care of these)
        routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
        routes.IgnoreRoute("{resource}.css/{*pathInfo}");

        //ControllerBuilder.Current.DefaultNamespaces.Add("Nexus.Controllers");

        routes.MapRoute(
            "testapi",
            "testapi/{controller}/{action}",
            new
            {
                controller = "User",
                action = "Get"
            },
            new[] { "Nexus.Controllers" }
        );

        routes.MapRoute(
            // Route name
            "Default",
            // URL with parameters
            "{controller}/{action}/{id}",
            // Parameter defaults
            new { controller = "Events", action = "Index", id = "" }
        );

    }

In my library project UserController:

namespace Nexus.Controllers
{
    public class UserController : ApiController
    {
        public Object Get(string id)
        {
            var guid = new Guid(id);

            var user = crm.Users.Where(x => x.UserId == guid).FirstOrDefault();

            return new
            {
                id = user.UserId,
                name = user.UserName,
                email = user.Email,
                extension = user.Ext,
                phone = user.Phone
            };
        }
    }
}

None of the URLs that I expect to work do: /testapi /testapi/User /testapi/User/Get /testapi/User/Get?id=1

They all return 404.

I've even used the route debugger to verify that the routes appear to be set up correctly: routes

What am I missing?

Was it helpful?

Solution

You seem to be confusing standard MVC controllers (deriving from the System.Web.Mvc.Controller class) and API controllers (deriving from the System.Web.Http.ApiController class). Your UsersController is an API controller.

So if you want to configure Web API routes you need to use the MapHttpRoute extension method and not the MapRoute which is only for standard MVC controllers.

Like this:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "testapi",
    routeTemplate: "testapi/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Also notice that in a RESTful API you do not specify the action name in the route. It is assumed from the HTTP verb used to consume it. Here's a good article I recommend you reading about RESTful routing in the Web API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top