Search code examples
c#asp.netasp.net-web-apiasp.net-web-api-routingattributerouting

ASP.NET Web API Route not found with Attribute Routing


I have an ASP.NET Web API project that contains multiple controllers. All controllers handle Models that exist in the database, but one. And this controller does not get his (one) Action resolved. Here is the controller:

[RoutePrefix("api/MobileStations")]
public class MobileStationsController : ApiController
{
    /// <summary>
    /// Gets all clients
    /// </summary>
    /// <returns>All clients</returns>
    [HttpGet]
    [ActionName(nameof(GetMobileStationsAsync))]
    [Route("", Name = "GetMobileStations")]
    public static async Task<IEnumerable<MobileStation>> GetMobileStationsAsync()
    {
        var snmpConfig = CiscoWlcSnmpHelpers.ReadSnmpConfiguration();

        var clients = await CiscoWlcSnmpHelpers.GetAllClientsWithAllAccessPointsFromAllWirelessControllersAsync(snmpConfig);

        return clients;
    }
}

I use Attribute Routing in all Controllers, with the exact same usage. Here is the Register Method from WebApiConfig.cs:

/// <summary>
/// Registers the config
/// </summary>
/// <param name="config"></param>
public static void Register(HttpConfiguration config)
{

    // Attribute routing
    config.MapHttpAttributeRoutes();

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

    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    config.Filters.Add(new ValidateModelAttribute());

    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
}

I used the Route Debugger with this result. URL is api/Mobilestations: Route not found

Excerpt of the Route selection table: Route not detected

So he uses the default route. Why is my custom route not detected on only this controller? It's the only one that is not accessing the database to get information. There is no table MobileStation in the DAL and I don't want to put an empty table into my database, just to get it to work. What does the routing engine care about the database?


Solution

  • Actions are not allowed to be static methods

    Which methods on the controller are considered "actions"? When selecting an action, the framework only looks at public instance methods on the controller. Also, it excludes "special name" methods (constructors, events, operator overloads, and so forth), and methods inherited from the ApiController class.

    Source : Routing and Action Selection in ASP.NET Web API: Action Selection

    Update action to be an instance method

    [RoutePrefix("api/MobileStations")]
    public class MobileStationsController : ApiController {
    
        /// <summary>
        /// Gets all clients
        /// </summary>
        /// <returns>All clients</returns>
        [HttpGet]
        [ActionName(nameof(GetMobileStationsAsync))]
        [Route("", Name = "GetMobileStations")] //GET api/MobileStations
        public async Task<IEnumerable<MobileStation>> GetMobileStationsAsync() { ... }
    }