Requirements
Preserve URL parameters
Create a link to the same page
In Razor page URLs are generated using Link.To(...)
.
By default calling Link.To()
without any parameters returns the base URL of the site. In this case it would be: https://blazor-cms.org/oqtane-tutorials
.
The key to retrieving the current page URL is to pass the current page parameters. Link.To(parameters: ...)
will then construct the URL based on the passed parameters.
Current page parameters are found in CmsContext.Page.Parameters
.
Link to current page with all Parameters
Add Parameters
Because MyPage.Parameters
/ CmsContext.Page.Parameters
follows the query string convention adding a new parameter isn't too difficult.
To add a new parameter you can use the .Add(key, value)
method or add the parameters as string following the convention as for example &name=2sxc
.
See current page parameters: basics320=page
Output
See adjusted page parameters: basics320=page&name=2sxc
Current page URL with new parameter from string: https://blazor-cms.org/oqtane-tutorials?basics320=page&name=2sxc
Current page URL with new parameter from .Add(...)
: https://blazor-cms.org/oqtane-tutorials?basics320=page&name=2sxc
Remove Parameters
To remove a parameter you can use the .Remove(key)
method or modify the string containing parameters.
See current page parameters: basics320=page
Output
See adjusted page parameters:
Current page URL with removed parameter: https://blazor-cms.org/oqtane-tutorials
Change Parameters
To change a parameter you can use the .Set(key, value)
method or modify the string containing parameters.
See current page parameters: basics320=page
Output
See adjusted page parameters: basics320=2sxc
Current page URL with changed parameter: https://blazor-cms.org/oqtane-tutorials?basics320=2sxc
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.RazorTyped @using ToSic.Razor.Blade; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Preserve URL parameters</h2> <h3>Create a link to the same page</h3> <p> In Razor page URLs are generated using <code>Link.To(...)</code>. By default calling <code>Link.To()</code> without any parameters returns the base URL of the site. In this case it would be: <code>@Link.To()</code>. </p> <p> The key to retrieving the current page URL is to pass the current page parameters. <code>Link.To(parameters: ...)</code> will then construct the URL based on the passed parameters. <br> Current page parameters are found in <code>CmsContext.Page.Parameters</code>. </p> </div> </div> <h2>Link to current page with all Parameters</h2> @{ var currentPageUrl = Link.To(parameters: MyPage.Parameters); } <ol> <li><a href='@Link.To()'>Link to current page <em>without</em> Parameters: @Link.To()</a></li> <li><a href='@currentPageUrl'>Link to current page preserving parameters @currentPageUrl</a></li> </ol> @* Snippet for the old v14 code, which shouldn't run but should be shown. This is a bit of a hack, but it works @{ var currentPageUrl = Link.To(parameters: CmsContext.Page.Parameters); } <ol> <li><a href='@Link.To()'>Link to current page <em>without</em> Parameters: @Link.To()</a></li> <li><a href='@currentPageUrl'>Link to current page preserving parameters @currentPageUrl</a></li> *@ <!-- unimportant stuff, hidden --> <p>See current page parameters: <code>@MyPage.Parameters</code></p> @{ // Example using string var newParamsFromString = MyPage.Parameters + "&name=2sxc"; // Page parameters using .Add(...) method var newParamsFromAdd = MyPage.Parameters.Add("name", "2sxc"); } <!-- unimportant stuff, hidden --> <p>Current page URL with new parameter from string: @Link.To(parameters: newParamsFromString)</p> <p>Current page URL with new parameter from <code>.Add(...)</code>: @Link.To(parameters: newParamsFromAdd)</p> @* Snippet for the old v14 code, which shouldn't run but should be shown. This is a bit of a hack, but it works @{ // Example using string var newParamsFromString = CmsContext.Page.Parameters + "&name=2sxc"; // Page parameters using .Add(...) method var newParamsFromAdd = CmsContext.Page.Parameters.Add("name", "2sxc"); } <!-- unimportant stuff, hidden --> <p>Current page URL with new parameter from string: @Link.To(parameters: newParamsFromString)</p> <p>Current page URL with new parameter from <code>.Add(...)</code>: @Link.To(parameters: newParamsFromAdd)</p> *@ <!-- unimportant stuff, hidden --> <p>See current page parameters: <code>@MyPage.Parameters</code></p> @{ var currentParamsRemoved = MyPage.Parameters.Remove("basics320"); } <!-- unimportant stuff, hidden --> <p>Current page URL with removed parameter: @Link.To(parameters: currentParamsRemoved)</p> @* Snippet for the old v14 code, which shouldn't run but should be shown. This is a bit of a hack, but it works @{ var currentParamsRemoved = CmsContext.Page.Parameters.Remove("basics320"); } <!-- unimportant stuff, hidden --> <p>Current page URL with removed parameter: @Link.To(parameters: currentParamsRemoved)</p> *@ <!-- unimportant stuff, hidden --> <p>See current page parameters: <code>@MyPage.Parameters</code></p> @{ var currentParamsChanged = MyPage.Parameters.Set("basics320", "2sxc"); } <!-- unimportant stuff, hidden --> <p>Current page URL with changed parameter: @Link.To(parameters: currentParamsChanged)</p> @* Snippet for the old v14 code, which shouldn't run but should be shown. This is a bit of a hack, but it works @{ var currentParamsChanged = CmsContext.Page.Parameters.Set("basics320", "2sxc"); } <!-- unimportant stuff, hidden --> <p>Current page URL with changed parameter: @Link.To(parameters: currentParamsChanged)</p> *@ @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })