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
:
Excerpt of the Route selection table:
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?