question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating URL with page parameter fails with new endpoint routing (regression from 2.1)

See original GitHub issue

This 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

  1. Create new 2.2 application from standard MVC template.
  2. Add an action to the HomeController:
    public IActionResult Test(int page)
    {
        return Json(new
        {
            Url = Url.Action(nameof(Test), new { page = 123 }),
            Page = page,
        });
    }
    
  3. Launch the application and open http://localhost:5000/Home/Test?page=4
  4. Observe the result which shows a generated URL /Home/Test?page=123.

Add Razor pages

  1. Now, create a Razor page at Pages/Foo.cshtml with the contents @page.
  2. Launch the application and open the previous URL again.
  3. Observe that the generated URL is now null.

Disable endpoint routing

  1. Disable endpoint routing by changing the AddMvc() call in Startup to set the compatibility level switch back to 2.1:
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
  2. Launch the application and open the previous URL again.
  3. 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
WistfulWolfcommented, Dec 14, 2018

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:

[ApiController, Route("api/[controller]")]
public class TestController : Controller
{
    [HttpGet(Name = "Test")]
    public void Test()
    {
    }
}

Page class:

public class AboutModel : PageModel
{
    public string Message { get; set; }

    public void OnGet()
    {
        string url = Url.Link("Test", new { page = 1 });
    }
}

The result is url = “http://localhost/api/Test Expected result is url = “http://localhost/api/Test?page=1

0reactions
rynowakcommented, Feb 23, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating URL with page parameter fails with new ...
This 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...
Read more >
c# - Url.Page() does not create a url for default handler with ...
Your best solution is to remove the parameters from the route template and let routing pass values in the query string. Then it...
Read more >
Routing in ASP.NET Core
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >
Routing in Razor Pages
Routing in Razor Pages is the system that matches URLs to Razor page files.
Read more >
Changelog | Cypress Documentation
Fixed a regression introduced in Cypress 12.12.0 where Cypress may fail to ... aligns with how the cy.visit() command generates urls with query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found