2016-05-29 4 views
0

私は、サービス要求を受け入れるために、次のアクションコードを持っている:ASP.NETのWebサービス、パラメータ文字列

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 
     } 
     //... 
    } 
} 
テストシナリオ

私はデバッガでだとブレークポイントが内のどこかに設定されています投稿メソッド。データセットでは、私はブランド名「3.1 Phillip Lim」を見つけました。私は投稿メソッドがヒットするようにこの名前(および他の同様のクリエイティブな名前)をどのように操作できるかを調べようとします。

ブレークポイントがヒットしない - 要求は 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内のセグメント...

どういうことが起こっているのか、どう対処したか説明してください。ありがとう。

答えて

0

あなたの問題はルートパラメータ内のドット文字に直接関係しているようです。これは既知の問題であり、IIS/Web APIが静的なファイル要求のようにクエリを解釈するという事実に依存します(ドット+拡張であると思われるため)。

何を試すことができます:

http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim/

  • は、あなたのリクエストURIの末尾にスラッシュを追加します。

    これは、Web APIがリクエストを処理するのに十分である必要がありますが、これは回避策のほうが多いと認めなければなりません。これをデフォルトの動作(this question)にするために、IISでリライトを作成することができます。これは十分で、それだけでは動作しない場合は、その後、以下の方法と組み合わせて、それを使用することができ

    <configuration> 
        <system.webServer> 
         <modules runAllManagedModulesForAllRequests="true" /> 
        </system.webServer> 
    </configuration> 
    

  • は、あなたのWeb.configファイルにRAMMFARを追加します。 performance implications when using 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の非友好的な文字(ドット、スペース、およびエンコードする必要がある任意の文字など)を含む可能性のあるパラメータをクエリ文字列に移動しようとします。もう1つの選択肢は、これらの文字をよりURIに優しい文字(ダッシュなど)で置き換えることです。

関連する問題