Aleksa Ristic
Aleksa Ristic

Reputation: 2499

Asp net core routing multiple parameters

I am trying to route asp.net core application.

I have having this scenario:

[Route("/{user}")]
public IActionResult FirstAction(string user)
{
    // Code
}

[Route("/{user}/{project}")]
public IActionResult SecondAction(string user, string project)
{
    // Code 2
}

Problem i am having is that when user goes to link /something it fires both methods (// Code 1 and // Code 2) but i want only first one to fire.

How can i achieve this?

Upvotes: 0

Views: 298

Answers (2)

johannespartin
johannespartin

Reputation: 501

Are you sure that both actions are fired? Which version of ASP NET Core are you using?

I tested the following:

    [HttpGet("/{user}")]
    public ActionResult<string> Test1(string user)
    {
        return "User " + user;
    }

    [HttpGet("/{user}/{project}")]
    public ActionResult<string> Test2(string user, string project)
    {
        return "User " + user + " Project " + project;
    }

The routes where explicit. If there was only one string Test1 was routed. If there were two strings Test2 was routed.


Furthermore, please note the following: If you put a leading / into the route any prefixes will be ignored.

Upvotes: 0

Dario Griffo
Dario Griffo

Reputation: 4274

Use a better REST API routing

[Route("/users/{user}")]

[Route("/users/{user}/projects/{project}")]

Upvotes: 1

Related Questions