【发布时间】:2017-04-22 17:20:21
【问题描述】:
我想使用 .net 核心 API 使用 url 查询参数而不是路径参数。
控制器
[Route("api/[controller]/[action]")]
public class TranslateController : Controller
{
[HttpGet("{languageCode}")]
public IActionResult GetAllTranslations(string languageCode)
{
return languageCode;
}
}
startup.cs 仅使用默认设置
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 Not found 所以很明显我的控制器路径设置不正确,但我不知道如何做到这一点......有什么想法吗?
我已尝试删除 [HttpGet("{languageCode}")] 属性,但我不断收到 null 参数而不是值。
【问题讨论】:
标签: asp.net-core asp.net-core-routing