Is it possible to reuse partial views for multiple projects in ASP.NET MVC?

I know that ASP.NET MVC 3 does not support area reuse, which would be very convenient, say, for user administration areas of web applications, but what about partial views?

Say I have a β€œcontrol” pager in the form of a Razor (or WebFormViewEngine , it doesn't matter) a partial view that I can easily reuse inside my MVC application.

Is it possible to reuse it in several MVC applications, except by creating a partial view in a new application and copying the code?

+4
source share
2 answers

There is nothing in the structure that you can do. You can take a look at MVCContrib portable areas that allow you to embed and reuse views between multiple ASP.NET MVC applications. You can also find the following blog post .

Disclaimer: Both of these approaches rely on creating a custom VirtualPathProvider that does not work with precompiled ASP.NET. Therefore, if you intend to pre-build your application before submitting, do not use it. Personally, I am in this situation, and what I ended up writing is a custom NuGet package that contains all the necessary views and assemblies containing their respective view models and all the developer has to do is install the NuGet package from a central location using the intranet / The Internet.

+7
source

Jesse Chavik's O'Reilly book, ASP.NET MVC 4 Programming, has a chapter that describes what you need. "CHAPTER 15 - Reusable Interface Components"

Basically, you create a class library project with your views. You must install RazorGenerator and install it as a Custom Tool in the properties of the .cshtml files. This will generate C # code from .cshtml files. Now, to find views outside the standard search path in your MVC application, you need to use the Nuget PrecompiledMvcEngine package.

The book is very well written and you can find step-by-step information on how to do this.

+4
source

Source: https://habr.com/ru/post/1342908/


All Articles