1
votes

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 }
        );
    }
}
2
You configuration is a little off. You are showing your route config for plain mvc not web api. - Nkosi
You're right! I suspected it was something tiny like this. Many thanks :) - Katya S

2 Answers

0
votes

Have a look at the following tutorial

Routing in ASP.NET Web API

using System.Web.Http;
using System.Net.Http;

namespace ERP.Controllers.DataAPI
{
  public class CommunityController : ApiController
  {
    PCSEntities db = new PCSEntities();

    public IEnumerable<Community> GetCommunities(Guid ApiKey)
    {
        return db.Communities.AsNoTracking().Take(10);
    }
  }
}

You are displaying route configuration for MVC not web Api

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.

        #region DataAPI

        var ns = new[] { "ERP.Controllers.DataAPI" };
        config.Routes.MapHttpRoute(
            name: "DataAPI",
            routeTemplate: "DataAPI/Community/{ApiKey}",
            defaults: new {controller= "Commmunity", action = "GetCommunities",  ApiKey = Guid.Empty},
            namespaces: ns
        );
        #endregion

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

Try to replace your route in RouteConfig as following:

routes.MapHttpRoute(
    name: "DataAPI",
    routeTemplate: "DataAPI/{controller}/{ApiKey}/{id}",
    defaults: new { ApiKey = Guid.Empty, id = UrlParameter.Optional }
);

In addition, with Web API you should not explicitly call Get method.

So in your case you can pass GUID as parameter like this: http://localhost/DataAPI/Community/6c4058a6-7edb-46c0-9d81-1c508c729f5b