vid*_*uch 3 asp.net-core asp.net-core-routing
我想使用url查询参数代替使用.net核心API的path参数。
控制者
[Route("api/[controller]/[action]")]
public class TranslateController : Controller
{
[HttpGet("{languageCode}")]
public IActionResult GetAllTranslations(string languageCode)
{
return languageCode;
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我想将GetAllTranslations更改为接受查询参数而不是路径参数,但是当我将邮递员查询更改为
http://localhost:42677/api/Translate/GetAllTranslations?languageCode=en
Run Code Online (Sandbox Code Playgroud)
我将收到错误404 Not Found,因此显然我的控制器路径设置不正确,但是我无法找到解决方法...有任何想法吗?
我试过删除[HttpGet(“ {languageCode}”))属性,但是我一直在获取null参数而不是值。
这就是你要找的
public IActionResult GetAllTranslations([FromQuery]string languageCode)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6854 次 |
| 最近记录: |