change redirecttoPage to pass unicode characters in Razor Pages

Joseph Kashishian 20 Reputation points
2025-05-30T14:31:58.9333333+00:00

I'm using a redirecttoPage with razor pages.

when the client page renders the / and # is not working and using the unicode

it appears /Clients/Update/1%2F%23military in the url

instead of

/Clients/Update/1#military

I'm using redirect below. is there way to convert; Is there anything I can do to tweak below.

return RedirectToPage("/Clients/Update", new { ClientID = this.Client.Client_ID.ToString() + "/#military" });

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,542 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,121 Reputation points
    2025-05-30T16:41:13.59+00:00

    The framework is encoding your string. %2F is a "/" and %23 is a "#". Look into routing templates with Razor Pages.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-9.0

    A target page that receives the redirect.

    @page "{id}/{bookmark}"
    @model RazorPagesDemo.Pages.Page2Model
    @{
    }
    <h1>Route Paramters</h1>
    <p>Id: @Model.id</p>
    <p>Bookmark: @Model.bookmark</p>
    

    Bind the route parameters to properties.

    [BindProperty(SupportsGet = true)]
    public int id { get; set; }
    [BindProperty(SupportsGet = true)]
    public string bookmark { get; set; } = string.Empty;
    

    The page that redirects to Page2

    public RedirectToPageResult OnGet()
    {
        return RedirectToPage("Page2", new { id = 42, bookmark = "#bookmark" });
    }
    

    Typically, the "#" is a bookmark on a page.

    https://www.w3schools.com/html/html_links_bookmarks.asp

    It is not clear from your post how the "#" is supposed to work.


  2. AgaveJoe 30,121 Reputation points
    2025-05-30T17:51:04.0666667+00:00

    The syntax for passing a bookmark is below. Keep in mind a bookmark or "fragment identifier" exists in the query string section of a URL.

     return RedirectToPage("Page2", null, new { id = 42 }, "bookmark");
    
    
    

    The result is...

    https://localhost:7103/Page2/42#bookmark
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.