在ASP.Net Web API Core中,可以使用查询字符串参数来路由到动作。以下是一个示例解决方法:
[HttpGet]
[Route("api/values")]
public IActionResult GetValues([FromQuery] string param)
{
// 根据查询字符串参数param执行相应的逻辑
// 返回结果
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRouting(options =>
{
options.LowercaseQueryStrings = true;
options.AppendTrailingSlash = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
http://localhost:port/api/values?param=value
其中,port
是应用程序运行的端口号,value
是要传递给查询字符串参数param
的值。
当请求到达时,ASP.Net Web API将自动将查询字符串参数绑定到控制器动作中的param
参数,并执行相应的逻辑。