I'll show you the issue with an example. I created a C# & ASP.NET Core 8 MVC (without HTTPS) sample project with Visual Studio. I added this code to HomeController.cs
:
[HttpGet]
[Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")]
public IActionResult Test()
{
return View();
}
And replaced Index.cshtml
with:
<a asp-action="PravaPristupa"
asp-route-firstId="33" asp-route-secondId="44">Test link</a>
The link that's generated on the index page seems to be incorrect (I didn't expect query parameters in the link): http://localhost:5229/Home/PravaPristupa?firstId=33&secondId=44
Next, I'm renaming the method name:
[HttpGet]
[Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")]
public IActionResult PravaPristupa()
{
return View();
}
And now the link is generated as expected: http://localhost:5229/Home/PravaPristupa/33/44
Why is it working this way, shouldn't the link generation be independent of the method name, and depend solely on the route setup?
I'll show you the issue with an example. I created a C# & ASP.NET Core 8 MVC (without HTTPS) sample project with Visual Studio. I added this code to HomeController.cs
:
[HttpGet]
[Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")]
public IActionResult Test()
{
return View();
}
And replaced Index.cshtml
with:
<a asp-action="PravaPristupa"
asp-route-firstId="33" asp-route-secondId="44">Test link</a>
The link that's generated on the index page seems to be incorrect (I didn't expect query parameters in the link): http://localhost:5229/Home/PravaPristupa?firstId=33&secondId=44
Next, I'm renaming the method name:
[HttpGet]
[Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")]
public IActionResult PravaPristupa()
{
return View();
}
And now the link is generated as expected: http://localhost:5229/Home/PravaPristupa/33/44
Why is it working this way, shouldn't the link generation be independent of the method name, and depend solely on the route setup?
Share Improve this question edited 2 days ago marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked 2 days ago MarkoMarko 1,6265 gold badges24 silver badges44 bronze badges 3 |1 Answer
Reset to default 0Because of you not specified what exactly route format you want to use the MVC interpreted the firstId
and secondId
as the query parameters. Using asp-route-<value>
values is only part of the data used for generating the targeting URL.
Therefore, it is necessary to define specific route template to be used by MVC to generate the required destination URL.
To make make this work add a name to the route by using the Name
parameter:
[Route("/Home/PravaPristupa/{firstId:int}/{secondId:int}", Name = "Test")]
public IActionResult SomeTest()
And then use the asp-route
tag:
<a asp-route="Test" asp-route-firstId="33" asp-route-secondId="44">Test link</a>
For example, in the old versions of MVC the @Html.RouteLink()
helper was very helpful in theses cases.
[Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")]
by[Route("/PravaPristupa/{firstId:int}/{secondId:int}")]
? – CompuChip Commented 2 days ago