ASP.NET Web API 找不到带有属性路由的路由
ASP.NET Web API Route not found with Attribute Routing
我有一个包含多个控制器的 ASP.NET Web API 项目。所有控制器都处理数据库中存在的模型,但只有一个。而且这个控制器没有得到他的(一个)动作解决。这是控制器:
[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;
}
}
我在所有控制器中都使用属性路由,用法完全相同。这是来自 WebApiConfig.cs 的 Register
方法:
/// <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;
}
我使用路由调试器得到了这个结果。 URL 是 api/Mobilestations
:
路线选择摘录table:
所以他使用默认路由。为什么只在这个控制器上没有检测到我的自定义路由?它是唯一一个不访问数据库来获取信息的。 DAL 中没有 table MobileStation,我不想将空的 table 放入我的数据库,只是为了让它工作。路由引擎关心数据库什么?
动作不允许是静态方法
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.
来源:Routing and Action Selection in ASP.NET Web API: Action Selection
将操作更新为实例方法
[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() { ... }
}
我有一个包含多个控制器的 ASP.NET Web API 项目。所有控制器都处理数据库中存在的模型,但只有一个。而且这个控制器没有得到他的(一个)动作解决。这是控制器:
[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;
}
}
我在所有控制器中都使用属性路由,用法完全相同。这是来自 WebApiConfig.cs 的 Register
方法:
/// <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;
}
我使用路由调试器得到了这个结果。 URL 是 api/Mobilestations
:
路线选择摘录table:
所以他使用默认路由。为什么只在这个控制器上没有检测到我的自定义路由?它是唯一一个不访问数据库来获取信息的。 DAL 中没有 table MobileStation,我不想将空的 table 放入我的数据库,只是为了让它工作。路由引擎关心数据库什么?
动作不允许是静态方法
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.
来源:Routing and Action Selection in ASP.NET Web API: Action Selection
将操作更新为实例方法
[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() { ... }
}