コントローラーに次のようなコードがあります:
[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
...しかし、次のようになります:
{
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メソッドは問題なく検出されます。なぜこのメソッドだけ検出されないのでしょうか? 他のメソッドと共通点がないのは、他のメソッドがHttpGetであるのに対し、このメソッドがHttpPostである点だけです。これはそれほど大きな違いがあるのでしょうか?午前この REST メソッドを呼び出そうとするときに、Postman のドロップダウンから「Post」を選択します。
アップデート
はい、どうやらこれは Post であることや URI で引数を渡すことには関係がないようです。なぜなら、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."
}
私はこれを次のように Postman から呼び出します:
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);
}
アップデート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
...そして、明確なパターンがあります。引数/パラメータを含むすべてのREST API呼び出しは、同じように失敗します。他のすべて(http://localhost:21609/api/deliveries/Count, http://localhost:21609/api/deliveryitems/count, http://localhost:21609/api/department/getall
)は正常に機能します。つまり、URIに引数がない場合、メソッドが検出されます。はargs、そうではありません。
どうやら、URIで引数/値を渡す方法に問題があるようです(「?=「引数が1つの場合は "、引数が2つの場合は "?=&=") および/またはルーティングの設定方法に問題があります。具体的には、次のスタイルが機能します。
[Route("api/Deliveries/Count")]
...一方、このスタイルでは次のことが起こりません:
[Route("api/delivery/invnumbyid/{ID}")]
できるだけ早くこの質問に賞金を出します。このゴルディエスクの結び目を解いた人には 50 ポイント、今日 (賞金の設定前) に回答すれば 100 ポイントです。
アップデート4
ルーティングを変更して、引数のデータ型を含めるようにしたので、次のようになります。
[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."
}
そこで、「ふざけて」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/
...次の条件に該当するにもかかわらず、「1 つ以上の必須パラメータに値が指定されていません。」というエラーが表示され、引き続き失敗します。
[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
return 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">
<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> IIS Web Core</td>
</tr>
<tr>
<th>Notification</th>
<td> MapRequestHandler</td>
</tr>
<tr class="alt">
<th>Handler</th>
<td> StaticFile</td>
</tr>
<tr>
<th>Error Code</th>
<td> 0x80070002</td>
</tr>
</table>
</div>
<div id="details-right">
<table border="0" cellpadding="0" cellspacing="0">
<tr class="alt">
<th>Requested URL</th>
<td> 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> C:\project\git\CStore\HHS.API\api\deliveryitems\InsertIntoPPTData\serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2</td>
</tr>
<tr class="alt">
<th>Logon Method</th>
<td> Anonymous</td>
</tr>
<tr>
<th>Logon User</th>
<td> Anonymous</td>
</tr>
<tr class="alt">
<th>Request Tracing Directory</th>
<td> C:\Users\clay\Documents\IISExpress\TraceLogFiles\HHS.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&IIS70Error=404,0,0x80070002,7601">View more information »</a>
</p>
</fieldset>
</div>
</div>
</body>
</html>
¡Que demonios!
いつも、ざわめくカーテンの後ろにもう 1 体のガーゴイルが隠れていることを、私は悲しくも微笑みながら思い出します (ロビンソン ジェファーズとエドガー アラン ポーが格闘しているように見えます)。青白くうろついています...
アップデート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);
}
...FirstId の値は 2、CountToFetch の値は 12 です (予想どおり)。しかし、Postman は次のような表記でパッケージを返送します。500 1 つ以上の必須パラメータに値が指定されていません。「
¡Que demonios! 明らかに両方のパラメータが渡されました!
より正しいと思われる URI を使用する場合:
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/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/department/getndeptsfromid
...そして、Postman の「URL パラメータ キー/値」インターフェースを使用してそれらを追加すると、次のように上記とまったく同じ URL が生成されます。
...そしてまったく同じ結果になりました。
もう一度だけ言います(「ラス・ウバス・デ・ラ・イラ「-インスピレーションを受けたスペイン語)」:¡Que demonios!
ベストアンサー1
問題は POST/GET とは関係なく、 でパラメータを指定する方法にのみ関係していますRouteAttribute
。これを保証するために、サンプルに両方の動詞のサポートを追加しました。
非常に単純な 2 つの動作例に戻りましょう。
[Route("api/deliveryitems/{anyString}")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsOne(string anyString)
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}
そして
[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsTwo(string anyString = "default")
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString);
}
最初のサンプルでは、「anyString
」はパス セグメント パラメータ (URL の一部) であることを示しています。
最初のサンプル URL は次のとおりです。
- ローカルホスト:
xxx/api/deliveryItems/dkjd;dslkf;dfk;kkklm;oeop
- 戻り値
"dkjd;dslkf;dfk;kkklm;oeop"
- 戻り値
2 番目のサンプルでは、" anyString
" はクエリ文字列パラメータであることを示しています (デフォルト値が提供されているため、ここではオプションですが、デフォルト値を削除するだけでオプションではなくなります)。
2 番目のサンプル例の URL は次のとおりです。
- ローカルホスト:
xxx/api/deliveryItems?anyString=dkjd;dslkf;dfk;kkklm;oeop
- 戻り値
"dkjd;dslkf;dfk;kkklm;oeop"
- 戻り値
- ローカルホスト:
xxx/api/deliveryItems
- 戻り値
"default"
- 戻り値
もちろん、次の 3 番目のサンプルのように、さらに複雑にすることもできます。
[Route("api/deliveryitems")]
[HttpGet, HttpPost]
public HttpResponseMessage GetDeliveryItemsThree(string anyString, string anotherString = "anotherDefault")
{
return Request.CreateResponse<string>(HttpStatusCode.OK, anyString + "||" + anotherString);
}
3 番目のサンプル例の URL は次のとおりです。
- ローカルホスト:
xxx/api/deliveryItems?anyString=dkjd;dslkf;dfk;kkklm;oeop
- 戻り値
"dkjd;dslkf;dfk;kkklm;oeop||anotherDefault"
- 戻り値
- ローカルホスト:
xxx/api/deliveryItems
- 「リクエスト URI に一致する HTTP リソースが見つかりませんでした...」を返します (パラメータ
anyString
は必須です)
- 「リクエスト URI に一致する HTTP リソースが見つかりませんでした...」を返します (パラメータ
- ローカルホスト:
xxx/api/deliveryItems?anotherString=bluberb&anyString=dkjd;dslkf;dfk;kkklm;oeop
- 戻り値
"dkjd;dslkf;dfk;kkklm;oeop||bluberb"
- パラメータが逆になっていることに注意してください。これは問題ではありません。最初の例の「URL スタイル」では不可能です。
- 戻り値
パス セグメントまたはクエリ パラメータはいつ使用すればよいでしょうか? ここではすでにいくつかのアドバイスが提供されています:REST API のベスト プラクティス: パラメータをどこに置くか?