C# 为什么会这样;找不到与请求URI匹配的HTTP资源;在这里

C# 为什么会这样;找不到与请求URI匹配的HTTP资源;在这里,c#,controller,postman,request-uri,http-post-vars,C#,Controller,Postman,Request Uri,Http Post Vars,我的控制器中有如下代码: [Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")] …我通过邮递员这样称呼它: http://localhost:21609/api/deliveryitems/InsertIntoPPTData? stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2 ht

我的控制器中有如下代码:

[Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")]
…我通过邮递员这样称呼它:

    http://localhost:21609/api/deliveryitems/InsertIntoPPTData?
stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2
http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12
…但是得到:

{
Message: "No HTTP resource was found that matches the request URI     
'http://localhost:21609/api/deliveryitems/InsertIntoPPTData?   stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2'."
MessageDetail: "No type was found that matches the controller named 'deliveryitems'."
}
以相同方式创建和调用的其他REST方法也很好——为什么不是这个呢?它与其他程序唯一不同的是它是一个HttpPost,而其他程序是HttpGet。这有那么大的区别吗?当我试图调用这个REST方法时,我从Postman的下拉列表中选择了“Post”

更新 是的,它显然与Post和在URI中传递args无关,因为我现在通过HttpGet方法得到了同样的结果:

{
Message: "No HTTP resource was found that matches the request URI     
'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."
MessageDetail: "No action was found on the controller 'Department' that matches the request."
}
我从邮递员那里这样称呼它:

    http://localhost:21609/api/deliveryitems/InsertIntoPPTData?
stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2
http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12
…它确实出现在我的控制器中:

[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
    return CCRService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
……为此:

String.Format("api/department/getndeptsfromid?firstId={0}&countToFetch={1}", FirstId, CountToFetch)
…但我还是得到了同样的错误消息

更新3 我发现这也不起作用:

http://localhost:21609/api/delivery/invnumbyid?ID=33
…因此有一个明确的模式。我所做的每个包含args/params的RESTAPI调用都以同样的方式失败;所有其他(
http://localhost:21609/api/deliveries/Count, http://localhost:21609/api/deliveryitems/count, http://localhost:21609/api/department/getall
)工作正常。注意,如果URI中没有参数,则会发现该方法。如果有args,则不是

因此,很明显,我在URI中传递arg/vals的方式(在单个arg的情况下附加“?=”,在两个arg的情况下附加“?=&=”)和/或设置路由的方式有问题。具体来说,这种风格的作品:

[Route("api/Deliveries/Count")]
…而这种风格不:

[Route("api/delivery/invnumbyid/{ID}")]
我会尽快给这个问题发奖金-50分给解开这个难缠的结的人,如果今天(在设立奖金之前)得到回答,100分

更新4 我更改了路由以包括arg的数据类型,因此:

[Route("api/delivery/invnumbyid/{ID}")]
-变成这样:

[Route("api/delivery/invnumbyid/{ID:int}")]"
但它还是失败了,咆哮着:

{
Message: "No HTTP resource was found that matches the request URI 
'http://localhost:21609/api/delivery/invnumbyid?ID=45'."
MessageDetail: "No action was found on the controller 'Delivery' that matches the request."
}
然后,“在云雀上”,我试着把这个输入邮递员:

http://localhost:21609/api/delivery/invnumbyid/45
http://localhost:21609/api/department/getndeptsfromid
…而且成功了!(我一直认为应该是“
http://localhost:21609/api/delivery/invnumbyid?ID=45
”)

但在《邮差》中也有类似的尝试:

http://localhost:21609/api/department/getndeptsfromid/2/12/
…继续失败,并且“一个或多个必需参数没有给定值”。即使遇到此问题:

[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
    return HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
“见鬼

我悲伤地微笑着记得,在沙沙作响的窗帘后面,总还有一个石像鬼藏着(罗宾逊·杰弗斯和埃德加·艾伦·坡似乎在那里摔跤)。苍白的游荡

更新5 好的,这里有不同的说法:

来自Postman,如果我使用此URL:

http://localhost:21609/api/department/getndeptsfromid/2/12/
我把它变成这个方法:

[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
    return HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
我得到:

{
Message: "No HTTP resource was found that matches the request URI   
'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."
MessageDetail: "No action was found on the controller 'Department' that matches the request."
}
但这是我提供的路线:

[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
…所以我想说,事实上,我在控制器上提供了一个匹配操作。顺便说一句,如果我只是将此作为URI输入到Postman中:

http://localhost:21609/api/delivery/invnumbyid/45
http://localhost:21609/api/department/getndeptsfromid
…然后使用Postman的“URL参数键/值”界面添加这些,它会生成与上面完全相同的URL,如下所示:

…结果完全一样


我只能再说一遍(请原谅我的“Las Uvas de la Ira”-灵感源自西班牙语):“恶魔!”

如果它是GET服务,那么您需要将它与GET方法一起使用,而不是POST方法。您的问题是类型不匹配。另一个类型不匹配的例子(将严重性放在透视图中)是尝试将字符串分配给整数变量。

您的问题与POST/GET无关,只与如何在
RouteAttribute
中指定参数有关。为了确保这一点,我在示例中添加了对这两个动词的支持

让我们回到两个非常简单的工作示例

[Route("api/deliveryitems/{anyString}")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsOne(string anyString)
{
    return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}
第三个示例URL是:

  • localhost:
    xxx/api/deliveryItems?anyString=dkjd;dslkf;dfk;kkklm;oeop
    • 返回另一个默认值“
  • 本地主机:
    xxx/api/deliveryItems
    • 返回“未找到与请求URI匹配的HTTP资源…”(参数
      anyString
      是必需的)
  • localhost:
    xxx/api/deliveryItems?另一个字符串=bluberb&anyString=dkjd;dslkf;dfk;kkklm;oeop
    • 返回“dkjd;dslkf;dfk;kkklm;oeop||bluberb”
    • 请注意,参数已反转,这无关紧要,第一个示例的“URL样式”不可能实现这一点
什么时候应该使用路径段或查询参数?这里已经给出了一些建议:

您需要映射唯一的路由,以将参数指定为查询元素。在RouteConfig.cs(或WebApiConfig.cs)中添加:


在通过查询字符串发送参数时,您是否尝试过使用
[FromUri]
属性

以下是一个例子:

[HttpGet]
[Route("api/department/getndeptsfromid")]
public List<Department> GetNDepartmentsFromID([FromUri]int FirstId, [FromUri] int CountToFetch)
{
    return HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
[HttpGet]
[路线(“api/部门/getndeptsfromid”)]
公共列表GetNDepartmentsFromID([FromUri]int-FirstId[FromUri]int-CountToFetch)
{
返回HHSService.GetNDepartmentsFromID(FirstId,CountToFetch);
}

使用System.Web.Http将此包也包括在顶部,

我遇到了这个问题,如果从另一个程序集调用REST方法,则必须确保所有引用的版本与主项目引用的版本相同,否则将永远找不到控制器


问候。

请检查您继承的课程。无论是简单的控制器还是APIController


我们可能会错误地从MVC5控制器创建一个控制器。它应该来自Web API控制器。

试试这个伴侣,你可以像这样把它扔进身体

    [HttpPost]
    [Route("~/API/ChangeTheNameIfNeeded")]
    public bool SampleCall([FromBody]JObject data)
    {
        var firstName = data["firstName"].ToString();
        var lastName= data["lastName"].ToString();
        var email = data["email"].ToString();
        var obj= data["toLastName"].ToObject<SomeObject>();

        return _someService.DoYourBiz(firstName, lastName, email, obj);
    }
[HttpPost]
[路由(“~/API/changetenameifneeded”)]
public bool SampleCall([FromBody]作业对象数据)
{
var firstName=data[“firstName”].ToString();
var lastName=data[“lastName”].ToString();
var email=data[“email”].ToString();
var obj=data[“toLastName”].ToObject();
return\u someService.DoYourB
config.Routes.MapHttpRoute(
            name: "MyPagedQuery",
            routeTemplate:     "api/{controller}/{action}/{firstId}/{countToFetch}",
            defaults: new { action = "GetNDepartmentsFromID" }
        );
[HttpGet]
[Route("api/department/getndeptsfromid")]
public List<Department> GetNDepartmentsFromID([FromUri]int FirstId, [FromUri] int CountToFetch)
{
    return HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);
}
    [HttpPost]
    [Route("~/API/ChangeTheNameIfNeeded")]
    public bool SampleCall([FromBody]JObject data)
    {
        var firstName = data["firstName"].ToString();
        var lastName= data["lastName"].ToString();
        var email = data["email"].ToString();
        var obj= data["toLastName"].ToObject<SomeObject>();

        return _someService.DoYourBiz(firstName, lastName, email, obj);
    }
    [HttpGet]
    [Route("students/list")]
    public StudentListResponse GetStudents(int? ClassId, int? GradeId)
    {
       ...
    }
    [HttpGet]
    [Route("students/list")]
    public StudentListResponse GetStudents(int? ClassId=null, int? GradeId=null)
    {
       ...
    }