I am trying to add an API Controller to an MVC5 application. However I'm getting an error "The RouteData must contain an item named 'action' with a non-empty string value." when I navigate to the route: http://localhost/DataAPI/Community/Get/
The application doesn't seem to want to recognise an API controller - if I add the action to the route defaults, I get a 404 error. When I replace it with a standard controller (and add default action to the route data) I can get the data. Am I missing something?
The controller lives in the Controllers/DataAPI subdirectory:
using System.Web.Http;
using System.Net.Http;
namespace ERP.Controllers.DataAPI
{
public class CommunityController : ApiController
{
PCSEntities db = new PCSEntities();
public IEnumerable<Community> Get(Guid ApiKey)
{
return db.Communities.AsNoTracking().Take(10);
}
}
}
This is my route config:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
#region DataAPI
var ns = new[] { "ERP.Controllers.DataAPI" };
routes.MapRoute(
name: "DataAPI",
url: "DataAPI/{controller}/{ApiKey}/{id}",
defaults: new { ApiKey = Guid.Empty, id = UrlParameter.Optional },
namespaces: ns
);
#endregion
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}