Home > Net >  Add fragment to RedirectToPage
Add fragment to RedirectToPage

Time:12-03

'Hi All

and sorry for my English.

Just trying to redirect to a page passing a fragment.

I'm unable to add fragment to RedirectToPage action. If I add "#" as part of string, it will be converted in # and therefore unuseful

PostAction

public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(AnagraficaClientiContatti).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AnagraficaClientiContattiExists(AnagraficaClientiContatti.AnagraficaClientiContattiID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage(
                        "../Index", 
                        new { id = Request.Query["idmaster"], pgm = Request.Query["pgm"], pgs1 = Request.Query["pgs1"], activetab = Request.Query["activetab"], searchstring = Request.Query["searchstring"] }
                        );
        }

and this work fine, but i need to redirect to above page with "#specificsection" at the end.

I tried to change the call in

return RedirectToPage(
                        "../Index", 
                        new { id = Request.Query["idmaster"], pgm = Request.Query["pgm"], pgs1 = Request.Query["pgs1"], activetab = Request.Query["activetab"], searchstring = Request.Query["searchstring"]   "#specificsection"    }
                        );

but browser return a "wrong" url with # instead #

?id=20&pgm=2&pgs1=2&activetab=custom-tabs-one-contatti&searchstring=text#specificsection

No luck, passing fragment as third or fourth parameter (with third null ) in RedirectToPage, as i understood from documentation.

Can someone help me to understand what i'm doing wrong?

Thanks in advance.

Best

Massimo

CodePudding user response:

Use the overload that takes a string?, string?, object?, string? for the page, page handler (which is null), route values and a fragment :

return RedirectToPage("/Index", null, new { 
    id = Request.Query["idmaster"], 
    pgm = Request.Query["pgm"], 
    pgs1 = Request.Query["pgs1"],
    activetab = Request.Query["activetab"], 
    searchstring = Request.Query["searchstring"]
},
"specificsection")
);
  • Related