How to use common layouts and styles in several MVC Asp.net applications

I have a visual studio solution with several telerik MVC4 shaving projects having the same look. I do not want to have the same layout, CSS / styles, images and js files copied to all MVC4 projects. What is the best way to do this reuse? I tried to create a commonUI project virtual directory and tried to reference _layout.cshtml using http: // localhost / ... in _ViewStart.cshtml, but it complained "http: /localhost/MyMvcApp/Views/Shared/_Layout.cshtml" is a valid virtual path. "

Please, help!

+6
source share
2 answers

Four recommendations:

1) Look at the areas, perhaps, instead of individual projects, are these really different components of the same system (admin, etc.)?

2) Use the addition of an existing element in visual studio and add elements via links. This still duplicates them for deployment, but you can keep one source.

3) Think about it by creating your own nuget package. That way, although you would copy the CSS and images that you would have packaged, and you could update the package as needed. The advantage is that you can update one project without relearning another (if they are separate and 1) not applicable).

4) I find it uglier than the rest, but in IIS I believe that you can display them in folders, so you can link to these files by links in your project, rather than deploying them there, and then display them in the corresponding folder at deployment time using a single source path.

I do not know how to split the path to the application.

EDIT:

I have never tried this before, so I can not vouch for its work, but it is possible to compile your common elements in a separate project, and then refer to the DLL in all other projects.

The link I found is this, but again, I have not confirmed that this works, just think that this could be a viable way to learn:

http://www.chrisvandesteeg.nl/2010/11/22/embedding-pre-compiled-razor-views-in-your-dll/

+10
source

Areas are bad because you cannot deploy them separately. Like you, I tried using virtual directories, and I was able to access the layouts using relative syntax:

@{ Layout = "~/Common/Layouts/Layout-001.cshtml"; } 

Above, the project layout inherits the layout in the / common / virtual directory in the root directory. As the overall layout evolves (along with bootstrap and jquery versions), you can use physical folders with named versions side by side (e.g. common.v01, common.v02,). Thus, you can upgrade your applications to a newer general scheme by changing the path to VD to the appropriate version.

The disadvantage of this is that you need to use IIS (not express) as a platform for development and testing.

0
source

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


All Articles