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:
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.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
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/