路由错误:找不到与请求URI匹配的HTTP资源 [英] Routing error: No HTTP resource was found that matches the request URI

查看:208
本文介绍了路由错误:找不到与请求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

这篇关于路由错误:找不到与请求URI匹配的HTTP资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆