Access precompiled views in asp.net core from another project / assembly

Following this question , I now set up precompiled views in my main asp.net application, which compiles to the DLL from the command line, using the following

dotnet razor-precompile

teams. Then I packed it as a nuget package using

dotnet pack

and added the package as a reference to the Ive project, from which the submissions were filmed. Then I created a new class that implements IViewLocationExpanderand sets this in the method of setup.csmy project, and I see that it is looking for a new location for the views. However, I do not know what to put as the search path for the precompiled view, since there are no .cshtml files there. I just get InvalidOperationExceptionwith a view not found.

Has anyone done this before or was able to suggest how can I add these precompiled views to the search path?

thank

+4
source share
1 answer

I was amazed, it worked directly like this:

ViewExpander:

services.AddMvc().AddRazorOptions(options =>
{
    options.ViewLocationExpanders.Clear();
    options.ViewLocationExpanders.Add(new TestViewLocationExpander());
};

:

public class TestViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (viewLocations == null)
        {
            throw new ArgumentNullException(nameof(viewLocations));
        }

        yield return "~/Test/Test.cshtml";
    }
}

*.PrecompiledViews.dll , Test/Test.cshtml.

, .

+1

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


All Articles