How to use common Layout and styles across multipl

2019-02-17 07:36发布

问题:

I have a visual studio solution with multiple telerik MVC4 razor projects having same look and feel. I don't want to have same layout, CSS/styles, images and js files copied in all MVC4 projects. What is the best way to accomplish this re-usability? I tried creating a virtual directory of a commonUI project and tried to refer _layout.cshtml using http://localhost/... in _ViewStart.cshtml but it complained saying "http:/localhost/MyMvcApp/Views/Shared/_Layout.cshtml' is not a valid virtual path."

Please help!

回答1:

Four recommendations:

1) Look into areas, maybe instead of having separate projects these are really different components of the same system (admin, etc.)?

2) Use add existing item in visual studio and add the items via links. This still duplicates them for deployment, but you can keep one source.

3) Consider doing this by building your own nuget package. That way although you would copy the CSS and images you would have it packaged up and could update the package as needed. The benefit is that you can update one project while not having to re-test the other (if they are separate and 1) doesn't apply).

4) I find this one uglier than the rest, but in IIS I believe you can map in folders, so you could refer to these files by links in your project, not deploy them there and then map in the appropriate folder at deployment time using a single source path.

I know of no way to share up the application path.

EDIT:

I have never tried this before, so can't vouch for it working, but maybe compile your shared items in a separate project and then reference the DLL in all other projects.

The reference I found is this, but again, I haven't confirmed that this works, just think this could be a viable path to explore:

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