How to redirect on ASP.Net Core Razor Pages

razor pages redirecttopage
razor page redirect to external url
asp.net core redirect to url
razor pages onget
razor pages routing
asp net core razor pages redirect to login
razor redirect to another view
razor pages areas

I am using the new Razor Pages in ASP.Net core 2 Now I need to redirect

I tried this, but the page does not redirect:

public class IndexModel : PageModel
{
    public void OnGet()
    {
        string url = "/.auth/login/aad?post_login_redirect_url=" + Request.Query["redirect_url"];

        Redirect(url);
    }
}

How to redirect?

You were very close. These methods need to return an IActionResult (or Task<IActionResult> for async methods) and then you need to return the redirect.

public IActionResult OnGet()
{
    string url = "/.auth/login/aad?post_login_redirect_url=" 
      + Request.Query["redirect_url"];

    return Redirect(url);
}

Razor pages documentation

However, you have a huge Open Redirect Attack because you aren't validating the redirect_url variable. Don't use this code in production.

Razor Pages Handler Method Action Results, ASP.NET Core includes more than three dozen ActionResult classes the content of a Razor page, returning the content of a file, redirecting to  I am using the new Razor Pages in ASP.Net core 2 Now I need to redirect I tried this, but the page does not redirect: public class IndexModel : PageModel { public void OnGet() { s

You can use the IActionResult to return a redirection or your razor page.

public IActionResult OnGet()
{
     if (!Auth())
     {
         return new RedirectToPageResult("/Portal/Login");
     }
     return Page();
}

How to redirect on ASP.Net Core Razor Pages, You were very close. These methods need to return an IActionResult (or Task<​IActionResult> for async methods) and then you need to return  Razor Pages is a new aspect of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. If you're looking for a tutorial that uses the Model-View-Controller approach, see Get started with ASP.NET Core MVC. This document provides an introduction to Razor Pages. It's not a step by step tutorial.

Same for pages without cs:

@page

@functions
{
    public IActionResult OnGet()
    {
        string url = "/.auth/login/aad?post_login_redirect_url=" 
          + Request.Query["redirect_url"];

        return Redirect(url);
    }
}

Redirecting in razor pages, In razor there are different redirect methods (all with various overloads): .mvc.​razorpages.pagebase.redirecttopage?view=aspnetcore-2.2. ASP.NET Core includes more than three dozen ActionResult classes covering a wide range of needs, including but not limited to executing and returning the content of a Razor page, returning the content of a file, redirecting to another resource or simply returning a specific HTTP status code.

PageModel.Redirect(String) Method (Microsoft.AspNetCore.Mvc , Creates a object that redirects () to the specified url. AspNetCore.Mvc.​RazorPages Mvc.RedirectResult Redirect (string url); member Is this page helpful? We can improve this by implementing the POST-REDIRECT-GET pattern, slightly modified for ASP.NET Core Razor Pages. The POST-REDIRECT-GET Pattern In order to implement PRG in our Razor Pages app, we make the search parameters inputs to our OnGet() method, and only do a redirect in OnPost() .

Introduction to Razor Pages in ASP.NET Core, In the preceding sample, it redirects to the root Index page ( /Index ). RedirectToPage is detailed in the URL generation for Pages section. With  This series of tutorials shows how to use Razor Pages in ASP.NET Core. Learn how to create a model, generate code for Razor pages, use Entity Framework Core and SQL Server for data access, add search functionality, add input validation, and use migrations to update the model.

Implementing POST-REDIRECT-GET in ASP.NET Core Razor Pages, NET Core Razor Pages. Let's see how to implement POST-REDIRECT-GET in our Razor Pages apps by changing OnGet and OnPost. ASP. When a Razor Pages application starts up, a collection of Attribute Routes (familiar to anyone who has used them in ASP.NET MVC 5 or MVC Core) is constructed, using the file and folder paths rooted in the Pages folder as the basis for each route's template. The standard Razor Pages 3.x site template includes three pages in the root folder:

Comments
  • Hi, I see you are a moderator in the RuSO. I was asked to bring some moderator attention to this meta question: meta.stackoverflow.com/q/394396/8620333
  • @TemaniAfif Thank you for info, we will look this theme.