2016-12-07 18 views
1

.netコアAPIを使用してパスパラメータの代わりにurlクエリパラメータを使用したいと思います。 asp .netコアを使用したクエリパラメータのルーティング

コントローラ

[Route("api/[controller]/[action]")] 
public class TranslateController : Controller 
{ 
    [HttpGet("{languageCode}")] 
    public IActionResult GetAllTranslations(string languageCode) 
    { 
     return languageCode; 
    } 
} 

startup.csこの enter image description here

私の郵便配達依頼はこちら enter image description here

であるように私の闊歩要求が見え

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddMvc() 
      .AddJsonOptions(jsonOptions => 
      { 
       jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; 
       jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
       jsonOptions.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
      }); 

    services.AddLogging(); 
    services.AddSingleton<IConfiguration>(Configuration); 

    services.AddSwaggerGen(c => 
    { 
     c.SingleApiVersion(new Info 
     { 
      Version = "v1", 
      Title = "Translate API", 
      Description = "bla bla bla description", 
      TermsOfService = "bla bla bla terms of service" 
     }); 
    }); 
} 

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
    loggerFactory.AddDebug(); 

    app.UseMvc(); 

    app.UseSwagger(); 
    app.UseSwaggerUi(); 
} 

デフォルト設定のみを使用している

私はクエリパラメータの代わりのパスパラメータを受け入れるように、私のGetAllTranslationsを変更したいが、私は

http://localhost:42677/api/Translate/GetAllTranslations?languageCode=en 

に私の郵便配達のクエリを変更するときにエラー404が見つかりません私は、明らかに私のコントローラパスが正しく設定されていないれますが、私はこれを行う方法を見つけることができません...任意のアイデア?

[HttpGet( "{languageCode}")]属性を削除しようとしましたが、値の代わりにnullパラメータを取得し続けます。

答えて

1

これは、あなたが

public IActionResult GetAllTranslations([FromQuery]string languageCode) 
+0

ありがとうございます。それはうまくいったが、asp.net web apiと比較して明示的に宣言しなければならない理由は何ですか? – vidriduch

0

@jcmontxから答えを探しているものでは働いたが、パラメータbindindが明示的に設定する必要がありますなぜそれが説明されていません。私はまだこれが強制されているのかどうか、そしてなぜこれが強制されているのか確信していますが、バインドパラメータが明示的に設定されていないと、意図していない方法でAPIを開きます。練習。

関連する問題