ASP.NET MVC迁移至ASP.NET Core 5:选择MVC还是Razor Pages及相关技术疑问咨询
Hey there! Let's break down your two questions with practical insights from working on large-scale ASP.NET Core migrations:
1. Is it feasible/recommended to write 20+ handler methods in a Razor Page's code-behind?
Short answer: It’s technically feasible, and can be a good fit— but keep maintainability top of mind.
Razor Pages natively supports multiple handler methods per page using patterns like On[HttpVerb][ActionName] (e.g., OnPostEditProfile, OnPostUpdateBilling, OnPostDeletePreference) or the explicit [HandlerMethod] attribute. For small, page-specific edit actions, this aligns perfectly with Razor Pages' page-centric design— keeping all logic tied to a single UI surface in one place, instead of spreading it across a controller and disconnected views.
That said, 20+ methods can make your PageModel feel bloated if you don’t organize it well. Here’s how to keep things clean:
- Extract repetitive logic (validation, data access, shared business rules) into separate service classes (you can even reuse services from your existing MVC app!) instead of duplicating it in handlers.
- Group related methods with region blocks (
#region UserProfileEdits/#endregion) to make the code easier to scan. - If some edit actions are complex enough to stand alone as distinct user flows, split them into separate Razor Pages— but only if it makes sense for the user experience, not just to split up code.
If your existing MVC controller already has 20+ actions for these edits, moving them to a Razor Page won’t be worse— in fact, it might make debugging easier, since all logic for the page lives in one file.
2. Can Razor Pages use existing models from class library projects?
Absolutely— this is not only feasible, it’s the recommended approach to avoid redundant work.
Razor Pages works seamlessly with existing .NET class libraries, whether they target .NET Standard, .NET Core, or even (for simple POCOs) .NET Framework. Here’s how to implement it:
- Add a project reference from your Razor Pages app to your model class library.
- Import your model namespace in your
PageModelor Razor view (use@using YourModelNamespaceat the top of a .cshtml file, or add it to_ViewImports.cshtmlfor global access). - Use the models exactly like you did in MVC— as properties on your
PageModel, handler method parameters, or directly in view markup.
Just double-check that your class library doesn’t depend on old ASP.NET Framework components like System.Web. If it does, refactor those parts to use ASP.NET Core equivalents, but pure model classes (POCOs, DTOs, validation attributes) should work without modification. Reusing these models will save you hours of rework and keep your data layer consistent throughout the migration.
As a final tip: If your team is already comfortable with MVC, upgrading to ASP.NET Core MVC will be the smoothest path. But if you want to simplify page-specific logic and cut down on "controller overhead" for these small edit scenarios, Razor Pages is a strong choice— especially since you can reuse all your existing models either way.
内容的提问来源于stack exchange,提问作者Sam

