1

I have a 10+ year old ASP.NET MVC app that I'm getting ready to update/upgrade to ASP.NET Core 5. I'm trying to make a decision whether to stick with the MVC approach which would probably make the upgrade process easier OR switch to Razor pages. This is a pretty large app and the models used in the app come from a library project in the solution.

With that said, I have two questions:

  1. Some of the pages in the old app allow for lots of small edit scenarios and they have corresponding action methods in the controller. I understand that with razor pages, I don't use controllers. Instead, all the code is in code behind pages. Is it possible/advisable to have 20+ action methods in code behind pages of razor pages. This action methods would support those edit scenarios.
  2. As mentioned above, all the models used in the MVC app come from a library project. I'm pretty sure the answer to this question is YES but I want to make sure that I can still do the same with Razor pages and use my existing classes from the library project as the models for my razor pages.
2
  • I think you should mix both MVC and razor page. The idea of razor page is separate the page logics from each other (each one is paired with the corresponding view). So that of course cannot be adapted fully from MVC because we know that some endpoints in MVC can be used in multiple pages/views. The amount of handlers are not limited and should be fine as long as they are all used for one page. Commented Feb 9, 2021 at 15:48
  • 1
    I will suggest you to check this answer on this SO question stackoverflow.com/a/48844411/3559462 which has detailed analysis, you can also check other answers Commented Feb 9, 2021 at 15:55

1 Answer 1

0

The code behind in Razor Pages is known as the PageModel file, and is a combined page controller and view model. It is intended to process requests for its partner content page (Razor view file). The standard request handler methods are named On[Http Verb Name] with an optional Async on the end e.g. OnGet or OnPostAsync. You can only have one of these per HTTP verb. However, you can have unlimited named handler methods where you insert a "name" after the verb and before Async e.g. OnPostUpdateEmailAsync, UpdateEmail being the "name".

You might use these because your page includes multiple forms and you want to separate the code that processes each one, rather than stuff your OnPost method with some kind of conditional logic to identify which form was posted. Or you might use them to process discrete AJAX edits in complex pages as you described.

If your multiple edit scenarios are driven by AJAX and work with JSON data, you can also consider using an API controller rather than named handler methods.

Razor Pages is built on MVC and is just as flexible and powerful as MVC. The main differences between the two are

  • the relationship between controller and view (1 to 1 instead of 1 to many)
  • the basis on which routes are constructed (based on page file paths)
  • a simpler and more logical (IMO) project folder structure

You can use existing .NET Framework libraries in .NET Core (Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project?) although you may prefer to port the library to .NET Core: https://learn.microsoft.com/en-us/dotnet/core/porting/

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

5 Comments

Thank you for this detailed explanation! One quick follow up question, if I may, how do I use, say a class from a .NET Standard library as the ViewModel for a razor page? For example, I have a complex PerformanceMonitor.cs class in my .NET Standard class library that I reference in my ASP.NET Core Razor Pages project and I want to use this PerformanceMonitor class as my ViewModel or at the very least, I'd like my ViewModel to use PerformanceMonitor as its base. How would I do this?
Basically, I would like the razor page ViewModels to wrap my domain objects
I could add a domain object as a property of my IndexModel but I'd then have to access the data or properties of my domain object by using the property name I assign to it. So, in the razor page, I could access my PerformanceMonitor as @Model.PerformanceMonitor.MyDataPoint. Is this the proper way of using domain objects in razor pages? I feel there must be a better way.
Yes, that's how you would do it. Same as if you added PerformanceMonitor as a property to an MVC ViewModel class.
Thank you! I appreciate your help!

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.