Reputation: 175
I have multiple views in a projects e.g. index.cshtml
, tutorials.cshtml
, blog.cshtml
etc...
I need a way to put each part of the code in these files into separate classes or methods for better maintenance what is your suggestion? (FYI: I'm using asp.net core 3.1)
Upvotes: 0
Views: 206
Reputation: 11554
You have two options for this case:
An Application Part is an abstraction over the resources of an app. Application Parts allow ASP.NET Core to discover controllers, view components, tag helpers, Razor Pages, razor compilation sources, and more. Using Application Parts, you can share an assembly (DLL) containing controllers, views, Razor Pages, razor compilation sources, Tag Helpers, and more with multiple apps. Sharing an assembly is preferred to duplicating code in multiple projects.
Check Microsoft documentation on the Application Part for more details.
Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence.
Check Microsoft documentation on the Razor Class Library for more details.
Upvotes: 1