How to load a razor page from code in asp net


ASP.NET is a popular programming language used for building web applications. One common task in ASP.NET is loading a page from code. In this article, we will explore how to accomplish this task with examples.

Loading a Razor Page from Code

To load a Razor page from code in ASP.NET, you can use the RedirectToPage . This method allows you to specify the name of the Razor page you want to load.

 IActionResult LoadRazorPage()
    return RedirectToPage("/MyRazorPage");

In the above example, the LoadRazorPage method the user to the MyRazorPage Razor page. The RedirectToPage method takes the path of the Razor page as a .

Data to a Razor Page

, you may need to pass data to the Razor page you are loading. You can achieve this by using the RedirectToPage method with additional parameters.

public IActionResult LoadRazorPageWithParameters()
    string name = "John";
    int age = 30;
    return RedirectToPage("/MyRazorPage", new { Name = name, Age = age });

In the above example, the LoadRazorPageWithParameters method redirects the user to the MyRazorPage Razor page and passes the Name and Age parameters. These parameters can be accessed in the Razor page using the @Page directive.

Accessing Passed Data in Razor Page

To access the passed data in the Razor page, you can use the @Page directive with the corresponding parameter names.

@model MyRazorPageModel

Welcome, @Model.Name!

You are @Model.Age old.

In the above example, the @Model.Name and @Model.Age variables are used to the passed data in the Razor page.


Loading a Razor page from code in ASP.NET is a common task. By using the RedirectToPage method, you can easily load a Razor page and pass data to it. This allows you to create and interactive web applications using ASP.NET.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents