属性路由不起作用
Attribute Routing doesn't work
我正在 visual studio Express 2013 中休息 api。
我有 2 个控制器:一个用于订单,一个用于客户。
我已经创建了以下内容:
/api/clients
获取所有客户的信息
/api/clients/1
获取 id = 1 的客户端信息
/api/orders/10
获取 id = 10 的订单信息
现在,我想创建这个:
/api/clients/1/orders
- 获取 id 为 1
的客户的所有订单信息
我读过有关属性路由的内容,但我无法让它发挥作用。
OrdersController.cs
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class OrdersController : ApiController
{
public Order Get(string id)
{
// ...
}
[Route("clients/{id}/orders")]
public List<Order> GetByClient(string id)
{
// ...
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Enable CORS
config.EnableCors();
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
像这样,所有其他请求都正常工作,但是当我尝试访问 /api/clients/1/orders
时,出现错误 HTTP Error 404.0 - Not Found. The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
但是一旦我取消注释 config.MapHttpAttributeRoutes();
,我就无法再访问任何请求 - 它们都是 return 这个:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
</StackTrace>
</Error>
我做错了什么?
取消注释您的 config.MapHttpAttributeRoutes()
行,然后在您的 Global.asax 文件中,替换为:
WebApiConfig.Register(GlobalConfiguration.Configuration);
有了这个:
GlobalConfiguration.Configure(WebApiConfig.Register);
您可以在这里阅读:
使用路由属性 [Route("clients/{id}/orders")]
,您应该访问操作 GetByClient()
而不是 /api/clients/1/orders
url,而是 /clients/1/orders
。要获得原始 url 只需修复路由:
[Route("api/clients/{id}/orders")]
public List<Order> GetByClient(string id)
我正在 visual studio Express 2013 中休息 api。
我有 2 个控制器:一个用于订单,一个用于客户。
我已经创建了以下内容:
/api/clients
获取所有客户的信息
/api/clients/1
获取 id = 1 的客户端信息
/api/orders/10
获取 id = 10 的订单信息
现在,我想创建这个:
/api/clients/1/orders
- 获取 id 为 1
我读过有关属性路由的内容,但我无法让它发挥作用。
OrdersController.cs
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class OrdersController : ApiController
{
public Order Get(string id)
{
// ...
}
[Route("clients/{id}/orders")]
public List<Order> GetByClient(string id)
{
// ...
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Enable CORS
config.EnableCors();
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
像这样,所有其他请求都正常工作,但是当我尝试访问 /api/clients/1/orders
时,出现错误 HTTP Error 404.0 - Not Found. The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
但是一旦我取消注释 config.MapHttpAttributeRoutes();
,我就无法再访问任何请求 - 它们都是 return 这个:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
</StackTrace>
</Error>
我做错了什么?
取消注释您的 config.MapHttpAttributeRoutes()
行,然后在您的 Global.asax 文件中,替换为:
WebApiConfig.Register(GlobalConfiguration.Configuration);
有了这个:
GlobalConfiguration.Configure(WebApiConfig.Register);
您可以在这里阅读:
使用路由属性 [Route("clients/{id}/orders")]
,您应该访问操作 GetByClient()
而不是 /api/clients/1/orders
url,而是 /clients/1/orders
。要获得原始 url 只需修复路由:
[Route("api/clients/{id}/orders")]
public List<Order> GetByClient(string id)