6

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 (e.g. 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

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I followed the above links and added the tools/dependencies to project.json. I also added the razor-precompile as a "postbuild" script, but now the view no longer exists in the project but in a shared DLL, it still isn't happy. Saying it can't find the view in the normal search locations. How do I reference a view in a common (referenced) assembly then?
Have you tried adding the shared library's namespace to _ViewStart.cshtml (or _ViewImports.cshtml)? @using Your.Namespace
Yes, It still doesn't find them. When I precompile them though it creates a <mynamespace>.precompiled.dll which I can't reference, only <mynamespace>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.