为什么“没有找到与请求URI匹配的HTTP资源"?这里? [英] Why is it that "No HTTP resource was found that matches the request URI" here?

查看:60
本文介绍了为什么“没有找到与请求URI匹配的HTTP资源"?这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器中有这样的代码:

[Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")]

...我是通过 Postman 调用它的:

 http://localhost:21609/api/deliveryitems/InsertIntoPPTData?stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2

...但是得到:

<代码>{消息:未找到与请求 URI 匹配的 HTTP 资源'http://localhost:21609/api/deliveryitems/InsertIntoPPTData?stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2'."MessageDetail:找不到与名为deliveryitems"的控制器匹配的类型.}

其他以相同方式创建和调用的 REST 方法也很好——为什么不是这个?它与其他人唯一不同的是它是一个 HttpPost,而其他人是 HttpGet.这有那么大的不同吗?尝试调用此 REST 方法时,我 正在从 Postman 的下拉列表中选择发布".

更新

是的,它显然与作为 Post 并在 URI 中传递 args 无关,因为我现在使用 HttpGet 方法得到相同的结果:

<代码>{消息:未找到与请求 URI 匹配的 HTTP 资源'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."MessageDetail:未在与请求匹配的控制器部门"上找到任何操作."}

我是像这样从邮递员那里打电话的:

http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12

...它确实出现在我的控制器中:

[HttpGet][Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]公共列表<部门>GetNDepartmentsFromID(int FirstId, int CountToFetch){返回 CCRService.GetNDepartmentsFromID(FirstId, CountToFetch);}

更新 2

当我更改这段代码时,我以为我即将解决它:

String.Format("api/department/getndeptsfromid/{0}/{1}", FirstId, CountToFetch)

...至此:

String.Format("api/department/getndeptsfromid?firstId={0}&countToFetch={1}", FirstId, CountToFetch)

...但我仍然收到相同的错误信息...

更新 3

我发现这也不起作用:

http://localhost:21609/api/delivery/invnumbyid?ID=33

...所以有一个明确的模式.我所做的每个包含 args/params 的 REST API 调用都以同样的方式失败;所有其他的 (http://localhost:21609/api/deliveries/Count, http://localhost:21609/api/deliveryitems/count, http://localhost:21609/api/department/getall) 工作得很好.IOW,如果URI中没有args,则发现该方法.如果有 参数,则没有.

所以显然我在 URI 中传递 args/vals 的方式有问题(在单个 arg 的情况下附加?=",在两个 args 的情况下)"?=&=") 和/或我设置路由的方式有问题.具体来说,这种风格有效:

[Route("api/Deliveries/Count")]

...而这种风格没有:

[Route("api/delivery/invnumbyid/{ID}")]

我会尽快给这个问题加分 - 50 分给这个 Gordiesque 结的解开者,如果今天(在设置赏金之前)回答了 100 分.

更新 4

我更改了路由以包含 arg 的数据类型,以便:

[Route("api/delivery/invnumbyid/{ID}")]

-变成这样:

[Route("api/delivery/invnumbyid/{ID:int}")]"

但还是失败了,咆哮着:

<代码>{消息:未找到与请求 URI 匹配的 HTTP 资源'http://localhost:21609/api/delivery/invnumbyid?ID=45'."MessageDetail:在与请求匹配的控制器‘Delivery’上没有找到任何操作."}

然后,在云雀上",我尝试将其输入 Postman:

http://localhost:21609/api/delivery/invnumbyid/45

...它奏效了!(我一直认为它需要是http://localhost:21609/api/delivery/invnumbyid?ID=45")

但是在 Postman 中也有类似的尝试:

http://localhost:21609/api/department/getndeptsfromid/2/12/

...继续失败,并显示没有为一个或多个必需参数提供值".即使这是命中:

[HttpGet][Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]公共列表<部门>GetNDepartmentsFromID(int FirstId, int CountToFetch){返回 HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);}

...并且 GetNDepartmentsFromID() 的参数具有预期的值(2 和 12)

更糟糕/更奇怪的是我得到的结果 "http://localhost:21609/api/deliveryitems/InsertIntoPPTData/serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2"现在:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><头><title>IIS 8.0 详细错误 - 404.0 - 未找到</title><style type="text/css"><!--body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;}代码{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}.config_source code{font-size:.8em;color:#000000;}前{margin:0;font-size:1.4em;word-wrap:break-word;}ul,ol{margin:10px 0 10px 5px;}ul.first,ol.first{margin-top:5px;}fieldset{padding:0 15px 10px 15px;word-break:break-all;}.summary-container fieldset{padding-bottom:5px;margin-top:4px;}Legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;}图例{color:#333333;;margin:4px 0 8px -12px;_margin-top:0px;字体粗细:粗体;字体大小:1em;}a:link,a:visited{color:#007EFF;font-weight:bold;}一个:悬停{文本装饰:无;}h1{font-size:2.4em;margin:0;color:#FFF;}h2{font-size:1.7em;margin:0;color:#CC0000;}h3{font-size:1.4em;margin:10px 0 0 0;color:#CC0000;}h4{font-size:1.2em;margin:10px 0 5px 0;}#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS",Verdana,sans-serif;颜色:#FFF;背景色:#5C87B2;}#content{margin:0 0 0 2%;position:relative;}.summary-container,.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}.content-container p{margin:0 0 10px 0;}#details-left{width:35%;float:left;margin-right:2%;}#details-right{width:63%;float:left;overflow:hidden;}#server_version{width:96%;_height:1px;min-height:1px;margin:0 0 5px 0;padding:11px 2% 8px 2%;color:#FFFFFF;背景颜色:#5A7FA5;边框底部:1px 实心#C1CFDD;边框顶部:1px 实心#4A6C8E;字体粗细:正常;字体大小:1em;颜色:#FFF;文本对齐:右;}#server_version p{margin:5px 0;}表{margin:4px 0 4px 0;width:100%;border:none;}td,th{vertical-align:top;padding:3px 0;text-align:left;font-weight:normal;border:none;}th{width:30%;text-align:right;padding-right:2%;font-weight:bold;}thead th{background-color:#ebebeb;width:25%;}#details-right th{width:20%;}表 tr.alt td,表 tr.alt th{}.highlight-code{color:#CC0000;font-weight:bold;font-style:italic;}.clear{clear:both;}.preferred{padding:0 5px 2px 5px;font-weight:normal;background:#006633;color:#FFF;font-size:.8em;}--></风格><身体><div id="内容"><div class="content-container"><h3>HTTP 错误 404.0 - 未找到</h3><h4>您要查找的资源已被删除、更名或暂时不可用.</h4>

<div class="content-container"><字段集><h4>最可能的原因:</h4><ul><li>指定的目录或文件在 Web 服务器上不存在.</li><li>该 URL 包含印刷错误.</li><li>自定义过滤器或模块,例如 URLScan,会限制对文件的访问.</li></fieldset>

<div class="content-container"><字段集><h4>你可以尝试的事情:</h4><ul><li>在 Web 服务器上创建内容.</li><li>查看浏览器 URL.</li><li>检查失败的请求跟踪日志并查看哪个模块正在调用 SetStatus.有关更多信息,请单击<a href="http://go.microsoft.com/fwlink/?LinkID=66439">这里</a>.</fieldset>

<div class="content-container"><字段集><h4>详细错误信息:</h4><div id="details-left"><table border="0" cellpadding="0" cellspacing="0"><th>模块</th><td>&nbsp;&nbsp;&nbsp;IIS Web Core</td></tr><tr><th>通知</th><td>&nbsp;&nbsp;&nbsp;MapRequestHandler</td></tr><th>Handler</th><td>&nbsp;&nbsp;&nbsp;StaticFile</td></tr><tr><th>错误代码</th><td>&nbsp;&nbsp;&nbsp;0x80070002</td></tr>

<div id="details-right"><table border="0" cellpadding="0" cellspacing="0"><th>请求的 URL</th><td>&nbsp;&nbsp;http://localhost:21609/api/deliveryitems/InsertIntoPPTData/serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2</td;</tr><tr><th>物理路径</th><td>&nbsp;&nbsp;C:projectgitCStoreHHS.APIapideliveryitemsInsertIntoPPTDataserNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2</tr><th>登录方法</th><td>&nbsp;&nbsp;&nbsp;匿名</td></tr><tr><th>登录用户</th><td>&nbsp;&nbsp;&nbsp;匿名</td></tr><th>请求跟踪目录</th><td>&nbsp;&nbsp;C:UsersclayDocumentsIISExpressTraceLogFilesHHS.API</td></tr><div class="clear"></div>

</fieldset>

<div class="content-container"><字段集><h4>更多信息:</h4>此错误表示该文件或目录在服务器上不存在.创建文件或目录并再次尝试请求.<p><a href="http://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,7601">查看更多信息&raquo;</a></p></fieldset>

¡Que Demonios!

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

更新 5

好的,这里有不同的表述:

来自 Postman,如果我使用这个 URL:

http://localhost:21609/api/department/getndeptsfromid/2/12/

我采用这种方法:

[HttpGet][Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]公共列表<部门>GetNDepartmentsFromID(int FirstId, int CountToFetch){返回 HHSService.GetNDepartmentsFromID(FirstId, CountToFetch);}

...其中 FirstId 的值为 2,CountToFetch 的值为 12(正如我所期望的那样).但是,邮递员发回了我的包裹,上面写着500 没有为一个或多个必需参数提供值."

¡ Que Demonios!两个参数明明都传了!

如果我使用看起来更正确的 URI:

http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12

我明白了:

<代码>{消息:未找到与请求 URI 匹配的 HTTP 资源'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."MessageDetail:未在与请求匹配的控制器部门"上找到任何操作."}

然而这是我提供的路线:

[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]

...所以我会说我确实在控制器上提供了一个匹配的操作.顺便说一句,如果我只是将其作为 URI 输入 Postman:

http://localhost:21609/api/department/getndeptsfromid

...然后使用 Postman 的URL Parameter Key/Value"接口添加这些,它生成与上面完全相同的 URL,如下所示:

...结果完全相同.

我只能再说一遍(原谅我以Las Uvas de la Ira"为灵感的西班牙语):¡Que Demonios!

解决方案

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

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

[Route("api/deliveryitems/{anyString}")][HttpGet、HttpPost]公共 HttpResponseMessage GetDeliveryItemsOne(string anyString){return Request.CreateResponse(HttpStatusCode.OK, anyString);}

还有

[Route("api/deliveryitems")][HttpGet、HttpPost]公共 HttpResponseMessage GetDeliveryItemsTwo(string anyString = "default"){return Request.CreateResponse(HttpStatusCode.OK, anyString);}

第一个示例表示anyString"是一个路径段参数(URL 的一部分).

第一个示例 URL 是:

第二个示例表示anyString"是一个查询字符串参数(这里是可选的,因为已经提供了默认值,但您可以通过简单地删除默认值来使其不可选).

第二个示例 URL 是:

当然,您可以让它变得更复杂,就像第三个示例一样:

[Route("api/deliveryitems")][HttpGet、HttpPost]public HttpResponseMessage GetDeliveryItemsThree(string anyString, string anotherString = "anotherDefault"){return Request.CreateResponse(HttpStatusCode.OK, anyString + "||" + anotherString);}

第三个示例 URL 是:

什么时候应该使用路径段或查询参数?这里已经给出了一些建议:REST API 最佳实践:参数放在哪里?>

I have code in my controller like so:

[Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")]

...and I'm calling it via Postman like so:

    http://localhost:21609/api/deliveryitems/InsertIntoPPTData?
stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2

...but get:

{
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'."
}

Other REST methods that are created and called in the same way are found just fine - why not this one? The only thing it does not have in common with the others is that it is a HttpPost, whereas the others are HttpGet. Does that make that much of a difference? I am selecting "Post" from the dropdown in Postman when trying to call this REST method.

UPDATE

Yes, it apparently has nothing to do with it being a Post and passing args in the URI, because I'm getting the same thing now with an HttpGet method:

{
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."
}

I'm calling it from postman like so:

http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12

...and it does indeed appear in my controller:

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

UPDATE 2

I thought I was about to solve it when I changed this code:

String.Format("api/department/getndeptsfromid/{0}/{1}", FirstId, CountToFetch)

...to this:

String.Format("api/department/getndeptsfromid?firstId={0}&countToFetch={1}", FirstId, CountToFetch)

...but I still get the same err msg...

UPDATE 3

I find that this also is not working:

http://localhost:21609/api/delivery/invnumbyid?ID=33

...and so there is a definite pattern. EVERY REST API call I make that contains args/params fails in this same way; all of the others (http://localhost:21609/api/deliveries/Count, http://localhost:21609/api/deliveryitems/count, http://localhost:21609/api/department/getall) work just fine. IOW, if there are no args in the URI, the method is discovered. If there are args, it is not.

So apparently there's either something wrong with how I'm passing the args/vals in the URI (appending "?=" in the case of a single arg and, in the case of two args "?=&=") and/or there's something wrong with how I set up the routing. Specifically, this style works:

[Route("api/Deliveries/Count")]

...whereas this style does not:

[Route("api/delivery/invnumbyid/{ID}")]

I will bountify this question ASAP - 50 points to the untangler of this Gordiesque knot, and 100 if it's answered today (prior to the setting up of the bounty).

UPDATE 4

I changed the routing to include the data type of the arg so that this:

[Route("api/delivery/invnumbyid/{ID}")]

-became this:

[Route("api/delivery/invnumbyid/{ID:int}")]"

But it still failed, growling:

{
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."
}

Then, "on a lark," I tried entering this into Postman:

http://localhost:21609/api/delivery/invnumbyid/45

...and it worked! (I had been thinking it needed to be "http://localhost:21609/api/delivery/invnumbyid?ID=45")

But a similar attempt in Postman:

http://localhost:21609/api/department/getndeptsfromid/2/12/

...continues to fail with "No value given for one or more required parameters." even though this is hit:

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

...and the args to GetNDepartmentsFromID() has the expected vals (2 and 12)

Even worse/weirder is the result I get with "http://localhost:21609/api/deliveryitems/InsertIntoPPTData/serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2" now:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>IIS 8.0 Detailed Error - 404.0 - Not Found</title>
        <style type="text/css">
            <!-- 
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;} 
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;} 
.config_source code{font-size:.8em;color:#000000;} 
pre{margin:0;font-size:1.4em;word-wrap:break-word;} 
ul,ol{margin:10px 0 10px 5px;} 
ul.first,ol.first{margin-top:5px;} 
fieldset{padding:0 15px 10px 15px;word-break:break-all;} 
.summary-container fieldset{padding-bottom:5px;margin-top:4px;} 
legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;} 
legend{color:#333333;;margin:4px 0 8px -12px;_margin-top:0px; 
font-weight:bold;font-size:1em;} 
a:link,a:visited{color:#007EFF;font-weight:bold;} 
a:hover{text-decoration:none;} 
h1{font-size:2.4em;margin:0;color:#FFF;} 
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.4em;margin:10px 0 0 0;color:#CC0000;} 
h4{font-size:1.2em;margin:10px 0 5px 0; 
}#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS",Verdana,sans-serif; 
 color:#FFF;background-color:#5C87B2; 
}#content{margin:0 0 0 2%;position:relative;} 
.summary-container,.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;} 
.content-container p{margin:0 0 10px 0; 
}#details-left{width:35%;float:left;margin-right:2%; 
}#details-right{width:63%;float:left;overflow:hidden; 
}#server_version{width:96%;_height:1px;min-height:1px;margin:0 0 5px 0;padding:11px 2% 8px 2%;color:#FFFFFF; 
 background-color:#5A7FA5;border-bottom:1px solid #C1CFDD;border-top:1px solid #4A6C8E;font-weight:normal; 
 font-size:1em;color:#FFF;text-align:right; 
}#server_version p{margin:5px 0;} 
table{margin:4px 0 4px 0;width:100%;border:none;} 
td,th{vertical-align:top;padding:3px 0;text-align:left;font-weight:normal;border:none;} 
th{width:30%;text-align:right;padding-right:2%;font-weight:bold;} 
thead th{background-color:#ebebeb;width:25%; 
}#details-right th{width:20%;} 
table tr.alt td,table tr.alt th{} 
.highlight-code{color:#CC0000;font-weight:bold;font-style:italic;} 
.clear{clear:both;} 
.preferred{padding:0 5px 2px 5px;font-weight:normal;background:#006633;color:#FFF;font-size:.8em;} 
-->
        </style>
    </head>
    <body>
        <div id="content">
            <div class="content-container">
                <h3>HTTP Error 404.0 - Not Found</h3>
                <h4>The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.</h4>
            </div>
            <div class="content-container">
                <fieldset>
                    <h4>Most likely causes:</h4>
                    <ul>
                        <li>The directory or file specified does not exist on the Web server.</li>
                        <li>The URL contains a typographical error.</li>
                        <li>A custom filter or module, such as URLScan, restricts access to the file.</li>
                    </ul>
                </fieldset>
            </div>
            <div class="content-container">
                <fieldset>
                    <h4>Things you can try:</h4>
                    <ul>
                        <li>Create the content on the Web server.</li>
                        <li>Review the browser URL.</li>
                        <li>Check the failed request tracing log and see which module is calling SetStatus. For more information, click 
                            <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>. 
                        </li>
                    </ul>
                </fieldset>
            </div>
            <div class="content-container">
                <fieldset>
                    <h4>Detailed Error Information:</h4>
                    <div id="details-left">
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr class="alt">
                                <th>Module</th>
                                <td>&nbsp;&nbsp;&nbsp;IIS Web Core</td>
                            </tr>
                            <tr>
                                <th>Notification</th>
                                <td>&nbsp;&nbsp;&nbsp;MapRequestHandler</td>
                            </tr>
                            <tr class="alt">
                                <th>Handler</th>
                                <td>&nbsp;&nbsp;&nbsp;StaticFile</td>
                            </tr>
                            <tr>
                                <th>Error Code</th>
                                <td>&nbsp;&nbsp;&nbsp;0x80070002</td>
                            </tr>
                        </table>
                    </div>
                    <div id="details-right">
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr class="alt">
                                <th>Requested URL</th>
                                <td>&nbsp;&nbsp;&nbsp;http://localhost:21609/api/deliveryitems/InsertIntoPPTData/serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2</td>
                            </tr>
                            <tr>
                                <th>Physical Path</th>
                                <td>&nbsp;&nbsp;&nbsp;C:projectgitCStoreHHS.APIapideliveryitemsInsertIntoPPTDataserNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2</td>
                            </tr>
                            <tr class="alt">
                                <th>Logon Method</th>
                                <td>&nbsp;&nbsp;&nbsp;Anonymous</td>
                            </tr>
                            <tr>
                                <th>Logon User</th>
                                <td>&nbsp;&nbsp;&nbsp;Anonymous</td>
                            </tr>
                            <tr class="alt">
                                <th>Request Tracing Directory</th>
                                <td>&nbsp;&nbsp;&nbsp;C:UsersclayDocumentsIISExpressTraceLogFilesHHS.API</td>
                            </tr>
                        </table>
                        <div class="clear"></div>
                    </div>
                </fieldset>
            </div>
            <div class="content-container">
                <fieldset>
                    <h4>More Information:</h4> 
  This error means that the file or directory does not exist on the server. Create the file or directory and try the request again. 

                    <p>
                        <a href="http://go.microsoft.com/fwlink/?LinkID=62293&amp;IIS70Error=404,0,0x80070002,7601">View more information &raquo;</a>
                    </p>
                </fieldset>
            </div>
        </div>
    </body>
</html> 

¡Que demonios!

I sadly smilingly remember that there is always one more gargoyle hiding behind the rustling curtain (where Robinson Jeffers and Edgar Allen Poe are seemingly wrestling). Palely loitering...

UPDATE 5

Okay, here it is stated differently:

From Postman, if I use this URL:

http://localhost:21609/api/department/getndeptsfromid/2/12/

I make it into this method:

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

...where the value of FirstId is 2, and the value of CountToFetch is 12 (as I would expect them to be). But then, Postman sends back my package with the notation, "500 No value given for one or more required parameters."

¡Que demonios! Both parameters were obviously passed!

If I use a URI that would seem more correct:

http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12

I get:

{
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."
}

Yet this is the route that I've provided:

[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]

...and so I would say that I did, in fact, provide a matching action on the controller.And, BTW, if I just enter this as the URI into Postman:

http://localhost:21609/api/department/getndeptsfromid

...and then use Postman's "URL Parameter Key / Value" interface to add those, it generates the exact same URL as above, as can be seen here:

...and with the exact same result.

I can only say it again (pardon my "Las Uvas de la Ira"-inspired Spanish): ¡Que demonios!

解决方案

Your problems have nothing to do with POST/GET but only with how you specify parameters in RouteAttribute. To ensure this, I added support for both verbs in my samples.

Let's go back to two very simple working examples.

[Route("api/deliveryitems/{anyString}")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsOne(string anyString)
{
    return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}

And

[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsTwo(string anyString = "default")
{
    return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}

The first sample says that the "anyString" is a path segment parameter (part of the URL).

First sample example URL is:

The second sample says that the "anyString" is a query string parameter (optional here since a default value has been provided, but you can make it non-optional by simply removing the default value).

Second sample examples URL are:

Of course, you can make it even more complex, like with this third sample:

[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsThree(string anyString, string anotherString = "anotherDefault")
{
    return Request.CreateResponse<string>(HttpStatusCode.OK, anyString + "||" + anotherString);
}

Third sample examples URL are:

When should you use path segment or query parameters? Some advice has already been given here: REST API Best practices: Where to put parameters?

这篇关于为什么“没有找到与请求URI匹配的HTTP资源"?这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆