3

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

3
  • /computer,mainboard,13 /computer/mainboard/13 Commented Dec 5, 2019 at 20:39
  • Did you try it? Commented Dec 5, 2019 at 21:21
  • Yes, maybe I'm doing something else wrong... I'm new to .net core Commented Dec 5, 2019 at 21:33

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

I think, routing parameters name MUST BE the same as constructor parameters name... (a,b,c) [HttpGet("somepath/{a},{b},{c}")] public IActionResult Test(string a, string b, int c)
What do you mean, exactly?
in your example code, could you write that: public IActionResult Test(string x, string y, int z)
No, the parameter names have to match. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.