路由错误:找不到与请求URI匹配的HTTP资源

路由错误:找不到与请求URI匹配的HTTP资源

问题描述:

我正在尝试进行API调用

I'm trying to make the API call

http://localhost:56578/v1/reports

调用我的GetReports()方法.

但是,我继续在该主题中收到错误消息.

However I continue to get the error message in the subject.

我通过路由前缀关注此处的ms docs:

I'm following the ms docs here via the route prefix:

我在做什么错了?

ReportV1Controller.cs

[Authorize]
[RoutePrefix("v1/reports")]
....
....
[Route("")]
public IHttpActionResult GetReports()

WebApiConfig.cs

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

对此进行更改:

[RoutePrefix("v1/reports")]

对此:

[RoutePrefix("api/v1/reports")]

由于:

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

请参见routeTemplate: "api/{controller}/{action}/{id}",您说所有路径的前缀将为api{controller}/{action}/{id}为占位符

See routeTemplate: "api/{controller}/{action}/{id}", you said prefix for all paths will be api, {controller}/{action}/{id} are placeholders

结论:如果要在任何地方使用v1前缀,请使用它代替api

Conclusion: if you are going to use v1 prefix everywhere, put it instead of api