Generating URL with page parameter fails with new endpoint routing (regression from 2.1)
See original GitHub issueThis appears to be a regression due to the new endpoint routing system in 2.2. As per aspnet/Mvc#7876, using parameter names action
and page
is somewhat difficult, however using a page
parameter inside an MVC action or using an action
parameter inside of a Razor page does work fine. At least it did.
With endpoint routing activated (through the MVC compatibility switch CompatibilityVersion.Version_2_2
), generating an action link that has a page
parameter will now fail iff Razor pages exist in the same project.
Steps to reproduce
Without Razor pages
- Create new 2.2 application from standard MVC template.
- Add an action to the
HomeController
:public IActionResult Test(int page) { return Json(new { Url = Url.Action(nameof(Test), new { page = 123 }), Page = page, }); }
- Launch the application and open
http://localhost:5000/Home/Test?page=4
- Observe the result which shows a generated URL
/Home/Test?page=123
.
Add Razor pages
- Now, create a Razor page at
Pages/Foo.cshtml
with the contents@page
. - Launch the application and open the previous URL again.
- Observe that the generated URL is now
null
.
Disable endpoint routing
- Disable endpoint routing by changing the
AddMvc()
call inStartup
to set the compatibility level switch back to 2.1:services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- Launch the application and open the previous URL again.
- Observe that the URL is generated properly again.
As you can see, endpoint routing breaks the URL generation here although the target URL exists just fine and although it also can be used properly the whole time (note that the JSON response always includes the passed page
value). The mere presence of Razor pages should not make the URL generation treat any page
parameter in a special way here.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (5 by maintainers)
Just encountered this issue in .NET Core 2.1. UrlHelper.Link when adding a parameter with name “page” completely ignores it. Tested on clean MVC project with an API controller.
Api Controller class:
Page class:
The result is url = “http://localhost/api/Test” Expected result is url = “http://localhost/api/Test?page=1”
This primary case reported here has been fixed by: https://github.com/aspnet/AspNetCore/pull/7441 for preview 3.
Note that there may still be a little bit of wierdness - in particular using
page
as a route parameter along with other route parameters may not preserve the ambient values.This issue is difficult to solve because fundamentally the routing system uses the presence or absence of these values in its decisions about what link to try and generate. I’m going to revisit the design of this and see what else I can come up with since this is a common problem. The changes in preview3 will hopefully make this a little bit better.