[英]ASP.NET WebService not hit, parameter string
我有以下操作代碼來接受服務請求:
namespace MyStuff.api
{
[RoutePrefix("api/Dy")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Authorize]
public class DyController : ApiController
{
//...
[Route("{pit}/{name}")]
public void Post(string pit, string name, [FromBody]JObject obj)
{
//...
#region do something
// work with name here
#endregion
}
//...
}
}
測試場景
我在調試器中,在Post方法內的某個位置設置了一個斷點。 在數據集中,我找到了一個品牌名稱“ 3.1 Phillip Lim”。 我試圖弄清楚如何操作此名稱(以及其他類似的創意名稱),以便Post方法得到成功。
斷點未命中-請求未到達Post
方法
- $.post("http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; }) - $.post("http://undisclosed.server.net/api/Dy/Brands/-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; }) - $.post("http://undisclosed.server.net/api/Dy/Brands/id-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; }) - $.post("http://undisclosed.server.net/api/Dy/Brands/body.Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; }) - $.post("http://undisclosed.server.net/api/Dy/Brands/{body.Name}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
斷點命中-請求如期到達
$.post("http://undisclosed.server.net/api/Dy/Brands/body-Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; }) $.post("http://undisclosed.server.net/api/Dy/Brands/{null}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
WebApi 2顯然在幕后做了一些事情-可能是URL中段的某種類型識別...
請說明發生了什么...或您如何解決此問題。 謝謝。
在我看來,您的問題與route參數中的點字符直接相關。 這是一個已知問題,並且取決於IIS / Web API將您的查詢解釋為靜態文件請求的事實(因為它以點號+似乎是擴展名的結尾)。
您可以嘗試的方法:
在請求URI末尾添加斜杠:
http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim/
。
這應該足以允許Web API處理請求,但是我必須承認這更多是一種解決方法。 您可以根據此問題在IIS中創建重寫以使其成為默認行為。
將RAMMFAR添加到您的Web.config
文件:
<configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> </configuration>
這可能就足夠了,但是如果不能單獨使用,則可以將其與以下方法結合使用。 使用RAMMFAR時也要注意性能的影響 。
為您的Web.config
任何路徑( *
)添加一個ExtensionlessUrlHandler
:
<remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
只需放棄就不要在尾隨路線參數中使用點。 通常,我嘗試將可能包含URI不友好字符(例如點,空格和必須編碼的任何字符)的參數移到查詢字符串中。 另一個選擇是簡單地將這些字符替換為對URI更友好的字符(例如破折號)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.