3

Given a library referenced in a project. That library has precompiled views (see this post how to achieve it). So the lib has ABC.dll and ABC.PrecompiledViews.dll assemblies.

There's a view in library inside /Views/Shared/Index.cshtml. And a controler which returns it.

Then I have an application references the library (both assemblies). MVC discovers and returns that Index view in runtime.

Now I create a view inside application in Views/Shared/Index.cshtml. So its relative name is the same as in referenced view. By I doing this I mean that I want to override the view from the library.

When application is started from VS (Ctrl-F5) it works but the view in application is ignored. When application is published (dotnet publish) then the application fails on start with the following error:

InvalidOperationException: The following precompiled view paths differ only in case, which is not supported:
/Views/Shared/Index.cshtml
/Views/Shared/Index.cshtml

Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler..ctor(IFileProvider fileProvider, RazorTemplateEngine templateEngine, CSharpCompiler csharpCompiler, Action compilationCallback, IList precompiledViews, ILogger logger)

Besides the problem that the view is ignored when I run from VS it's meaningful behavior. The app has two similar view and can't understand which one to choose.

So the question is how to force MVC to use a particular view (from app instead of from lib)?

There's a method to separate views with same relative names when they are belong to different controllers. Here's nice discussion - Restrict route to controller namespace in ASP.NET Core But my case is a bit different.

Shrike
  • 9,218
  • 7
  • 68
  • 105
  • Why are you precompiling views if you don't want them precompiled? – Charles Nov 28 '17 at 14:06
  • @Charles It's a library, it contains default views, but in some application people can want to override them. – Shrike Nov 28 '17 at 14:09
  • Why not let the consumers precompile them if necessary then? – Charles Nov 28 '17 at 14:10
  • @Charles How a lib can contain views? Only embedded or precompiled. Otherwise I have to distribute view files in lib's nuget package. which isn't ideal – Shrike Nov 28 '17 at 14:14
  • I was suggesting distributing the view files. If you don't want to include them, then you'll probably just need to leverage routing to not use generic paths like this. Using MVC to only serve a single index page is a little silly. That's what app.UseDefaultFiles() is for. – Charles Nov 28 '17 at 14:17
  • @Charles distibuting view files is bad because of difficulties in updating - a new package version will contain updated files but they won't be copied as they exist already – Shrike Nov 28 '17 at 14:20

0 Answers0