How to make a Redirect to the controller method from the RazorPageModel?

Volk Volk 571 Reputation points
2023-12-21T10:39:22.29+00:00

Hi!

The question is simple, but I'm stuck on it.

I have a RazorPageModel in Identity\Pages\RazorPageModel.cshtml.cs in which this happens:

public async Task<IActionResult> OnPostAsync()         {

	...

	return RedirectToPage("~/ControllerName/ControllerMethodName", new { info = n });

	or

	return Redirect("~/ControllerName/ControllerMethodName?info=" + n);
}

There is no way I can get into the Controller Method Name method and pass the data there.

I tried it in different ways, HTTP ERROR 404 crashes.

How to make a Redirect to the controller method from the RazorPageModel?

Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 2,330 Reputation points Microsoft Vendor
    2023-12-22T03:17:58.9833333+00:00

    Hi @Volk Volk,

    Here is a whole working demo you could follow:

    1.Test.cshtml.cs in Areas\Identity\Pages

    public class TestModel : PageModel
    {
        public async Task<IActionResult> OnGet()
        {
            return RedirectToAction("Index","Sales", new { area = "Customer",info = 1234 }); ;           
        }
    }
    

    2.Index.cshtml in Areas\Customer\Views\Sales and SalesController in Areas\Customer\Controllers

    User's image

     [Area("Customer")]
     public class SalesController : Controller
     {
         public IActionResult Index(int info)
         {
             return View();
         }
     }
    

    3.Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container...
    builder.Services.AddControllersWithViews();
    builder.Services.AddRazorPages();
    
    
    var app = builder.Build();
    
    //other middleware....
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    
    app.MapControllerRoute(
        name: "MyArea",
        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    app.MapRazorPages();
    
    app.Run();
    

    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,
    Rena

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,166 Reputation points
    2023-12-21T11:02:42.3966667+00:00

    I assume you are trying to call an MVC controller Form a Razor Page. First, MVC must be configured.

    builder.Services.AddMvc();
    
    //
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller}/{action}/{id?}");
    

    Then redirecting is simply.

    public IActionResult OnGet()
    {
        return RedirectToAction("Index", "Home");
    }
    

    MVC configuration is covered in the documentation.

    1 person found this answer helpful.