Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect from one Razor Page to another

I have an Page called Page1.

Inside of OnGet(string parameter1) I check a parameter, and in some case want to route the user to another Page.

The Page is located here:

Pages/App/App.cshtml

I have tried this:

 this.RedirectToPage("/App/App");//

But the user does not get redirected. It just shows the same page as expected if the redirect was not there. I want them to see the App page.

So how do I redirect to the App page?

like image 310
Greg Gum Avatar asked Oct 18 '25 07:10

Greg Gum


2 Answers

This is what worked:

public async Task<IActionResult> OnGet(string web_registration_key)
{
  //Check parameter here

    if(doRedirect)
    {
       return RedirectToPage("/App/App")
    }

  }

  return null;

}

The return null seems odd, but not sure what else to return.

like image 60
Greg Gum Avatar answered Oct 19 '25 23:10

Greg Gum


I used the return Page();

Here is an example from on of my projects:

    public IActionResult OnGet(int id)
    {
        var MenuItemFromDb = _db.MenuItem.Include(m => m.CategoryType).Include(m => m.FoodType)
                .Where(x => x.Id == id).FirstOrDefault();

        if (MenuItemFromDb == null)
        {
            return RedirectToPage("./Index");
        }
        else
        {
            ShowCart(id);
            return Page();
        }
    }

    private void ShowCart(int id)
    {
        var MenuItemFromDb = _db.MenuItem.Include(m => m.CategoryType).Include(m => m.FoodType)
            .Where(x => x.Id == id).FirstOrDefault();

        CartObj = new ShoppingCart()
        {
            MenuItemId = MenuItemFromDb.Id,
            MenuItem = MenuItemFromDb
        };
    }
like image 21
Dragon Mastery Avatar answered Oct 19 '25 22:10

Dragon Mastery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!