最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - asp-net-route tag doesn't generate the expected link - Stack Overflow

programmeradmin3浏览0评论

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
  • What happens if you replace [Route("[controller]/PravaPristupa/{firstId:int}/{secondId:int}")] by [Route("/PravaPristupa/{firstId:int}/{secondId:int}")]? – CompuChip Commented 2 days ago
  • /Home segment gets dropped from the generated URL, but otherwise the behavior remains the same (for both cases). – Marko Commented 2 days ago
  • Generally, route values that don't match the route parameters are put in a query string. Please see this link for additional info on how route values are mapped: learn.microsoft/en-us/aspnet/core/mvc/controllers/… – SoftwareDveloper Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Because 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.

发布评论

评论列表(0)

  1. 暂无评论