asp net core convert mvc to razor pages

Asp.net Core: Seamless Migration from MVC to Razor Pages

By Mark Lewis
AD

Embarking on the journey to convert your ASP.NET MVC application to Razor Pages? You're in the right place. This comprehensive guide will walk you through the process, making it a smooth and efficient transition.

Before we dive in, let's briefly understand why you might want to make this shift. Razor Pages, introduced in ASP.NET Core 2.0, provides a page-focused framework, simplifying the mapping between a request and a response. It's particularly useful for scenarios where you want a tight coupling between the UI and the code-behind logic.

Preparation: Understand the Differences

Familiarizing yourself with the key differences between MVC and Razor Pages is crucial before starting the conversion. MVC offers a clear separation of concerns, promoting code organization and testability. On the other hand, Razor Pages provides a more conventional, server-side WebForms-like model.

AD
A Developer’s Guide to ASP.NET Core Razor Pages
A Developer’s Guide to ASP.NET Core Razor Pages
ASP.NET Core MVC Training in Vadodara - ATI
ASP.NET Core MVC Training in Vadodara - ATI
a screenshot of an open source program
a screenshot of an open source program

Understanding these differences will help you decide which approach fits your project's needs best and, consequently, how to structure your Razor Pages.

Razor Page vs MVC Controller

In Razor Pages, the page itself is the controller, handling requests and generating views. In contrast, an MVC application separates these concerns into controllers and views. This key difference impacts how you organize and navigate your application logic.

the asp net core info sheet shows what it is like to work on an application
the asp net core info sheet shows what it is like to work on an application
Building the ASP.net Core Razor Pages site – CORS tutorial | Microsoft Community Hub
Building the ASP.net Core Razor Pages site – CORS tutorial | Microsoft Community Hub
ASP.NET Core Route Tooling
ASP.NET Core Route Tooling

For instance, instead of an MVC controller action, you'll have a `OnGet`, `OnPost`, etc., methods in your Razor Page to handle different HTTP verbs. This might seem unusual at first, but it provides a straightforward and linear flow for handling requests.

Folders and Routing

Razor Pages uses a file system-based routing model versus the attribute-based routing in MVC. It means that your folder structure directly influences your URL structure. A page located at `/Pages/About.cshtml` will correspond to the `about` route.

AD

This can lead to cleaner URLs and easier navigation. Still, you'll need to adjust your project structure and folder hierarchy to take full advantage of this feature.

Asp.net Core Roadmap For Beginners
Asp.net Core Roadmap For Beginners
ASP.NET Core Web Development for Beginners: A Practical Guide to Building Real Applications with MVC, Razor Pages, Entity Framework Core, and SQL
ASP.NET Core Web Development for Beginners: A Practical Guide to Building Real Applications with MVC, Razor Pages, Entity Framework Core, and SQL
#dotnet #csharp #dependencyinjection #scrutor #cleanarchitecture #dotnetcore #enterpriseapps #developers #softwarearchitecture #codingbestpractices #microservices #scalablesystems #devtips… | Shaheen Aziz | 17 comments
#dotnet #csharp #dependencyinjection #scrutor #cleanarchitecture #dotnetcore #enterpriseapps #developers #softwarearchitecture #codingbestpractices #microservices #scalablesystems #devtips… | Shaheen Aziz | 17 comments
How to Publish ASP.NET Core MVC Project.NET 8 with View Files
How to Publish ASP.NET Core MVC Project.NET 8 with View Files
Understanding OOPS in ASP.NET Core | MOHAMED HASIF M H posted on the topic | LinkedIn
Understanding OOPS in ASP.NET Core | MOHAMED HASIF M H posted on the topic | LinkedIn
the complete asp net core q2 2016 chat sheet
the complete asp net core q2 2016 chat sheet

Converting MVC Controllers to Razor Pages

Now that you're well-versed with the differences, let's discuss the actual conversion process. Start by identifying the controllers in your MVC application. For each controller, you'll create a corresponding Razor Page.

Let's consider a simple example. If you have an `AboutController` in your MVC application, you'll create a new Razor Page named `About.cshtml` in the `Pages` folder:

AD
  1. Create the Razor Page: Right-click on the `Pages` folder, select Add > Razor Page, and name it About.cshtml.
  2. Update the Routing Configuration: Open the Startup.cs file and add or update the routing configuration as follows:
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
        

Converting Actions to Page Methods

With the Razor Page created, you'll need to convert the controller's actions into corresponding methods in the code-behind file (e.g., `About.cshtml.cs`). Follow these steps:

GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
  • For each action in your AboutController, create the corresponding method in the About.cshtml.cs file, replacing the HTTP verb with the appropriate method suffix (e.g., OnGet, OnPost).
  • Update the method bodies with the corresponding logic from your controller.

Optimizing View Content

One advantage of Razor Pages is that the view is naturally integrated into the page file. However, managing view content can become cumbersome as your application grows. Luckily, partial views and shared layouts can help keep your code clean.

Extract commonly used UI components (e.g., headers, footers, navigational menus) into partial views and use them across your application. For larger projects, consider creating a separate Layout.cshtml file to maintain a consistent design across your pages.

AD

And there you have it! You've successfully converted your ASP.NET MVC application to Razor Pages. Embrace the tight coupling between UI and logic, and enjoy the streamlined workflow it offers. Now, it's time to take your application to the next level. Happy coding!

AD