.netコアAPIを使用してパスパラメータの代わりにurlクエリパラメータを使用したいと思います。 asp .netコアを使用したクエリパラメータのルーティング
コントローラ
[Route("api/[controller]/[action]")]
public class TranslateController : Controller
{
[HttpGet("{languageCode}")]
public IActionResult GetAllTranslations(string languageCode)
{
return languageCode;
}
}
であるように私の闊歩要求が見え
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パラメータを取得し続けます。
ありがとうございます。それはうまくいったが、asp.net web apiと比較して明示的に宣言しなければならない理由は何ですか? – vidriduch