ASP.. NET Web API错误:没有找到与请求URL匹配的HTTP资源

本文关键字:URL 请求 资源 HTTP API Web NET 错误 ASP | 更新日期: 2023-09-27 18:12:34

我检查了其他在这里提问和回答的解决建议,但仍然找不到我自己的代码有什么问题。

我有一个'成员'和'fals'(意味着类似blogpost)控制器。每个成员都有自己的多个'fals'记录,所以我想列出'fals'属于特定的成员。

我想在我的Web API中添加一个自定义操作来完成这项工作,它是:

    [HttpGet, ActionName("fals"), Route("members/{id}/fals")]
    public IQueryable<Fal> fals(int id)
    {
        Member member = db.Members.Find(id);
        return member.Fals();
    }

那么,这里是WebApiConfig定制:

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

我知道有两个路由,即使我注释或取消注释第二个路由,也不会改变任何东西。

当我尝试调用http://localhost:51601/api/members/1/fals URL时,它说:

{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:51601/api/members/1/fals'.",
"MessageDetail": "No action was found on the controller 'Members' that matches the name 'fals'."}

ID 1存在,当我粘贴代码时,fals存在,但无法计算出来。

谢谢!

ASP.. NET Web API错误:没有找到与请求URL匹配的HTTP资源

我不确定你是否使用属性路由,但尝试从控制器动作中删除Route属性。

[HttpGet, ActionName("fals")]

如果路由"干扰"了常规路由,我希望这能起作用,或者你也可以尝试调用没有/api/部分的URL,因为它从你在属性中指定的路由中丢失了。