Search code examples
c#asp.net-coreasp.net-core-routing

Routing in ASP.NET Core without a 'slash'


All routing examples are using the / character, for example:

/{category}/{product}/{id} for /computer/mainboard/13

Is it possible to use a comma ,, instead of /? For example:

/{category},{product},{id} for /computer,mainboard,13


Solution

  • Yes, it is possible.

    I tested on ASP.NET Core 2.2.

    TestController.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}");
        }
    }
    

    StartUp.cs:

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
        }
    }
    

    Program.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>();
    }
    

    When opening http://localhost:5000/test/somepath/abc,def,123 I get the expected output:

    a: abc, b: def, c: 123