Reputation: 2499
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
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
Reputation: 4274
Use a better REST API routing
[Route("/users/{user}")]
[Route("/users/{user}/projects/{project}")]
Upvotes: 1