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 an absolute URL while ignoring all ambient values

See original GitHub issue

Is there a simple way to generate an URL to the current Razor page, while ignoring all ambient values?

Let’s assume I’m on a Razor page Foo that has an optional route parameter id, @page {id?}. So my current URL is http://localhost:5000/Foo/Bar. Now I want to generate a link to that same Razor page but without the id. The desired result would be http://localhost:5000/Foo.

All my attempts to use the IUrlHelper failed because it will always reuse the ambient values, even when I specify an explicit RouteValueDictionary. That’s because the UrlHelper will eventually use the link generator to create an address, and that will always extract the ambient values first.

The only way I got this to work now is the following:

public static string GetPageUri(this HttpContext httpContext, string page)
{
    var linkGenerator = httpContext.RequestServices.GetService<LinkGenerator>();
    var values = new RouteValueDictionary
    {
        ["page"] = page,
    };
    return linkGenerator.GetUriByAddress(httpContext, new RouteValuesAddress
    {
        ExplicitValues = values,
    }, values);
}

So I use the link generator and the most low-level function it offers to create an URI by a RouteValuesAddress where I explicitly leave out the ambient values.

Am I missing something or is there really no way to generate a link like that from the UrlHelper directly? I am aware that reusing ambient values for the same page is completely per design but there should still be some way to opt out of this, or not?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Dec 13, 2019

@poke I see.

I’m going to close the issue for now as the question has been answered. I’ll let @rynowak comment on the fragment below if he has any further thoughts and we can potentially open a separate issue if necessary to track any remaining work.

I would want this on the UrlHelper, so that I can use it directly within a view without having to retrieve a link generator.

0reactions
rynowakcommented, Dec 16, 2019

That’s good. The UrlHelper does feel a bit old at times.

Super yes. The problem is that we have many many experiences that are layered on top of it, and these areas are super touchy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent ambient route values from the URL from being ...
Ambient values that don't match a parameter are ignored, and ambient values are also ignored when an explicitly provided value overrides it, ...
Read more >
Routing in ASP.NET Core
The ambient route values, URL base path, scheme, and host from ... GetUriByAddress, Generates an absolute URI based on the provided values.
Read more >
Routing to controller actions in ASP.NET Core
Routing attempts to use the values in ambient values to fill in information that wasn't provided when generating a URL.
Read more >
The Anchor Tag Helper in Razor Pages
It can also used to generate absolute URLs to external resources. ... providing a value, all subsequent ambient values will be ignored.
Read more >
Fix your Ambient Occlusion map in Adobe Substance Painter
An error occurred while retrieving sharing information. Please try again later.
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