简体   繁体   中英

Share views (.cshtml files) between projects in asp.net core solution

I have a project which for various reasons have a number of controllers and views that are essentially the same. At the moment they are replicated between projects so there are several copies of each in each project. I was wondering if there was anyway to put these in a separate project (eg a class library project) and amend the View Locator capabilities (IViewLocationExpander?) or some other means to get it to look in a different project for these views.

I know it was possible in “full” asp.net with a bit of hacking about and it was never very clean or elegant, so wondered if there was any better way with asp.net core?

Thanks

You could create a AspNetCore Class Library and use AspNetCore Precompiled Views . Also as indicated in this StackOverflow answer . That should allow you to compile and share the Controllers and Views as you want. The views (.cshtml files) would no longer need to be deployed as they would be compiled and available in this class library.

Then either add a reference to this class library project (if in the same solution) or create it as a Nuget package and add it to the other projects. (This can be done by a private Nuget server or Local Nuget repository folder).

Update: ASP.NET Core 2.1 has a new Razor SDK which makes this simpler. It emits a Views assembly (AppName.Views.dll).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM