4

Currently, I found that the .net core has the .net core Razor Page Application and .net core MVC application. Here I have some questions related to it.

  • What is the use of the Razor page application? Because it is similar to MVC.
  • Which are the benefits of the Razor page application on the MVC?
  • Which one is faster?
  • Can anyone suggest that when we need to use the Razor page and when to use the MVC?

2 Answers 2

10

Functionally, there's no difference between a Razor Page application and an MVC application. ASP.NET Core is ASP.NET Core. You can use all of MVC, Razor Pages, APIs and Razor Components (i.e. Blazor) all within the same application if you wanted. The project templates serve to simply get you started with one way or another, but there's no lock in.

As far as Razor Page vs MVC approaches goes, Razor Pages essentially have no controller. They follow the MVVM (Model-View-View Model) pattern, whereas MVC appropriately enough follows the MVC (Model-View-Controller) pattern. Essentially, with a Razor Page, the "controller" from MVC is built into the page's model.

When to use which is mostly a stylistic preference. MVC is more flexible and works in all use cases. Razor Pages are slightly simpler, but also more limited in utility. Namely, they only support GET and POST methods and mostly only return views (i.e. the cshtml portion of the Razor Page). You can sort of get them to return JSON and such, but they're really not suited for that. If you have a Razor Page that requires retrieving data via AJAX, it's better to create an MVC/API-style controller for that.

One final benefit to Razor Pages is that they are self-contained and discreet, whereas MVC tends to be more nebulous (multiple actions returning multiple different views). They work great for things that are self-contained. For example, ASP.NET Core's Identity Default UI utilizes Razor Pages like Register, Login, ResetPassword, etc., where all the logic for each individual thing is one Razor Page. In an MVC approach, you'd have something like an AccountController which would have all the logic for everything, as well as a set of disconnected views to service each action individually. It's not that the MVC approach is bad or wrong, but Razor Pages is more digestable here. However, again, this is all about style, not anything critical one way or another.

Finally, to sum up, neither is "faster" or "better". It's just personal preference.

Sign up to request clarification or add additional context in comments.

2 Comments

> Namely, they only support GET and POST methods and mostly only return views Incorrect on all of that. Handler methods for any HTTP verb can be added.
I have read that .NET MVC 5 has support for razor. Does this mean razor pages or razor syntax? I assume it is not the same as razor pages.
2

From Introduction to Razor Pages Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views.

Razor Pages vs ASP.NET Core MVC has an opinionated presentation but also include links to the best posts on the topic.

We've discovered the best way to make this decision is to do some non-trivial development with both approaches.

I saw no significant advantage of Razor Pages (RP) over MVC with controllers and views when I ported Get started with ASP.NET Core MVC to Get started with Razor Pages in ASP.NET Core. It wasn't until I ported Get started with EF Core in an ASP.NET MVC web app to Razor Pages with Entity Framework Core in ASP.NET Core that I saw a significant productivity advantage. MVC/EF is by no means close to a real production app, it's much simpler. Yet it has enough complexity to demonstrate the advantages of RP over MVC.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.