c# - 在没有 'slash' 的 ASP.NET Core 中路由

标签 c# asp.net-core asp.net-core-routing

所有路由示例都使用/字符,例如:

/{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/

相关文章:

c# - ASP.net Core API 中的路由

c# - 如何解决 ASP.NET Core MVC 中的 'View not found' 错误?

c# - Flickr API key 存储

c# - Asp.net Core 5.0 在使用时似乎没有 httpContext.Session.GetString 作为方法

linux - 在后台运行 K Kestrel

asp.net-mvc - 如何将 ASP.net 身份角色放入 Identityserver4 身份 token 中

asp.net - ASP .NET Core : Routing. 开头的 url 中有多个类别

c# - ExpectedConditions.InvisibilityOfElementLocated 需要更多时间

c# - LINQ 和引用列表问题

c# - 从用 C# 编写的 Windows 服务访问映射的文件夹