简体   繁体   English

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

[英]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.

相关问题 我收到错误“没有找到与请求 URI 匹配的 HTTP 资源” - I am getting error “No HTTP resource was found that matches the request URI” 在 WebAPI 和 AngularJS 中出现错误:“未找到与请求 URI 匹配的 HTTP 资源” - Getting the error: “No HTTP resource was found that matches the request URI” in WebAPI and AngularJS Web Api错误:“找不到与请求URI匹配的HTTP资源” - Web Api Error: “No HTTP resource was found that matches the request URI” 未找到与Angular 2中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in Angular 2 找不到与请求URI&#39;MyUrl&#39;匹配的HTTP资源 - No HTTP resource was found that matches the request URI 'MyUrl' 获取没有与请求URI匹配的HTTP资源 - Getting No HTTP resource was found that matches the request URI 找不到与请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI 找不到与webapi中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in webapi 未找到与请求URI匹配的HTTP资源,未找到与控制器匹配的类型 - No HTTP resource was found that matches the request URI, No type was found that matches the controller 自定义“找不到与请求uri匹配的http资源”消息 - Customizing “No http resource was found that matches the request uri” message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM