[英]Routing error: No HTTP resource was found that matches the request URI
I'm trying to make the API call 我正在尝试进行API调用
http://localhost:56578/v1/reports
to call my GetReports()
method. 调用我的
GetReports()
方法。
However I continue to get the error message in the subject. 但是,我继续收到该主题中的错误消息。
I'm following the ms docs here via the route prefix: 我在这里通过路由前缀关注ms docs:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-prefixes https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-prefixes
What am I doing wrong? 我究竟做错了什么?
ReportV1Controller.cs ReportV1Controller.cs
[Authorize]
[RoutePrefix("v1/reports")]
....
....
[Route("")]
public IHttpActionResult GetReports()
WebApiConfig.cs WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Change from this: 从此更改:
[RoutePrefix("v1/reports")]
to this: 对此:
[RoutePrefix("api/v1/reports")]
because of: 因为:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
See routeTemplate: "api/{controller}/{action}/{id}"
, you said prefix for all paths will be api
, {controller}/{action}/{id}
are placeholders 参见
routeTemplate: "api/{controller}/{action}/{id}"
,您说所有路径的前缀都是api
, {controller}/{action}/{id}
是占位符
Conclusion: if you are going to use v1
prefix everywhere, put it instead of api
结论:如果要在任何地方使用
v1
前缀,请将其代替api
What you have should work provided that you have enabled attribute routing in the WebApiConfig
只要您已在
WebApiConfig
启用属性路由,那么您应该拥有的工作
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Attribute routing.
config.MapHttpAttributeRoutes(); //<-- THIS HERE
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Reference Attribute Routing in ASP.NET Web API 2: Enabling Attribute Routing ASP.NET Web API 2中的参考属性路由: 启用属性路由
And assuming 并假设
[Authorize]
[RoutePrefix("v1/reports")]
public class ReportV1Controller : ApiController {
//GET v1/reports
[Route("")]
[HttpGet]
public IHttpActionResult GetReports() {
//...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.