Force all Urls having a query parameter in ASP.NET Core

198 views Asked by At

Is there a way to force all Urls built using the UrlHelper to include a specific parameter? The idea would be once I set a specific parameter to always follow it on links.

I generate most of my urls using the following code: Url.RouteUrl("MyRoute", new {myparam = 1})

What I would like to do: I go to https://example.com?myparam=1 I would like all links generated on the page to include the myparam=1 parameter. If no param has been specified, then the url should not include it.

I am thinking on extending UrlHelper, overriding the RouteUrl method (I only use this one), extend the parameters and register this class as a service, but is seems complicated as parameters are passed as an anonymous class...

Is there any better way to reach that behavior?

1

There are 1 answers

3
Mark Redman On

You could use a session variable or a cookie to do that, then use as required. You solution seems quite a bit riskier in something going wrong.