所有路由示例都使用/
字符,例如:
/{category}/{product}/{id}
为了
/computer/mainboard/13
是否可以使用逗号 ,
代替 /
?例如:
/{category},{product},{id}
for /computer,mainboard,13
最佳答案
是的,这是可能的。
我在 ASP.NET Core 2.2 上测试过。
测试 Controller .cs:
[Route("test")]
public class TestController :Controller
{
[HttpGet("somepath/{a},{b},{c}")]
public IActionResult Test(string a, string b, int c)
{
return Ok($"a: {a}, b: {b}, c: {c}");
}
}
启动.cs:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
}
}
程序.cs:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
当打开 http://localhost:5000/test/somepath/abc,def,123
时,我得到了预期的输出:
a: abc, b: def, c: 123
关于c# - 在没有 'slash' 的 ASP.NET Core 中路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202910/