What does OnGetRedirectToPage do? ASP.NET Core Razor Pages

Shervan360 1,661 Reputation points
2023-07-10T15:56:57.8766667+00:00

Hello,

What does OnGetRedirectToPage do?

I expected this method to redirect the Test to the Privacy page. But it didn't do anything.

Screenshot 2023-07-10 085045

@page
@model Web_6.Pages.TestModel
@{
}

<h2>In Test</h2>

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Web_6.Pages
{
     public class TestModel : PageModel
     {
          //public IActionResult OnGet()
          //{
          //     return RedirectToPage("/Privacy");
          //}
          public IActionResult OnGetRedirectToPage() 
               // It doesn't redirect to Privacy and still in Test page.
          {
               return RedirectToPage("/Privacy");
          }
     }
}

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-07-10T17:35:35.81+00:00

    Most likely the OnGetRedirectToPage() handler is not executing. You did not share how TestModel is called Assuming TestModel is in the root, the URL would be...

    <a href=/Test?handler=RedirectToPage>Call Redirect</a>

        public class TestModel : PageModel
        {
            public void OnGet()
            {
            }
    
            public IActionResult OnGetRedirectToPage()
            {
                return RedirectToPage("/Privacy");
            }
        }
    
    @page
    @model RazorPagesDemo.Pages.TestModel
    <a asp-page-handler="RedirectToPage">Redirect</a>
    

    https://www.learnrazorpages.com/razor-pages/handler-methods

    https://www.mikesdotnetting.com/article/308/razor-pages-understanding-handler-methods

    https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-7.0&tabs=visual-studio

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-07-10T19:14:56.5066667+00:00

    Try the following

    public class IndexModel : PageModel
    {
        public IActionResult OnGet() => new RedirectToPageResult("/Privacy");
    }
    
    0 comments No comments

  2. Xinran Shen - MSFT 2,091 Reputation points
    2023-07-11T07:30:56.44+00:00

    Hi @Shervan360,

    In Razor Page, The action name is composed of two parts. The first part is On + http verb and the second part is the real name of this action. For example, Your action name is OnGetRedirectToPage, It just like the below method in normal mvc project:

    [HttpGet]
    public IActionResult RedirectToPage(){...}
    

    In your project, The default OnGet() is for rendering the page, If you want to redirect via OnGetRedirectToPage action, You need to call this action in Test page. Because OnGetRedirectToPage is a HttpGet method, So you can just use <a></a> to call it. Use asp-page-handler taghelper to sepecify the real name of the target action,

    <a asp-page-handler="RedirectToPage">redirect</a>
    

    After you click this <a> tag, OnGetRedirectToPage will be called, Then return RedirectToPage("Privacy"); will rediret to the page successfully.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Xinran Shen

    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.