1

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?

5
  • What URL are you trying to access the endpoint on?
    – Craig H
    CommentedMar 23, 2017 at 16:41
  • oh yeah sure, I forgot. It's api/mobilestations (exactly what the routeprefix is saying). I updated my question with the URL @CraigH
    – user4344677
    CommentedMar 23, 2017 at 17:43
  • 2
    Can attribute routing be applied to static methods?
    – Sunshine
    CommentedMar 23, 2017 at 18:21
  • I think Sunshine has hit the nail on the head.
    – Craig H
    CommentedMar 23, 2017 at 19:27
  • 1
    Oh what a blunder. Didn't even thought about it. A lesson to be careful with code analysis tools like StyleCop and their recommendations. Thank you @Sunshine
    – user4344677
    CommentedMar 27, 2017 at 14:02

1 Answer 1

3

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() { ... } }