Search code examples
asp.netrazorasp.net-coreclass-library

Razor engine can't find my view in .Net Core class library


I have my class library, which consists of ViewRenderService class:

public interface IViewRenderService
    {
        Task<string> RenderToStringAsync(string viewName, object model);
    }

    public class ViewRenderService : IViewRenderService
    {
        private readonly IRazorViewEngine _razorViewEngine;
        private readonly ITempDataProvider _tempDataProvider;
        private readonly IServiceProvider _serviceProvider;

        public ViewRenderService(IRazorViewEngine razorViewEngine,
            ITempDataProvider tempDataProvider,
            IServiceProvider serviceProvider)
        {
            _razorViewEngine = razorViewEngine;
            _tempDataProvider = tempDataProvider;
            _serviceProvider = serviceProvider;
        }

        public async Task<string> RenderToStringAsync(string viewName, object model)
        {
            var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            using (var sw = new StringWriter())
            {
                var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

                if (viewResult.View == null)
                {
                    throw new ArgumentNullException($"{viewName} does not match any available view");
                }

                var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = model
                };

                var viewContext = new ViewContext(
                    actionContext,
                    viewResult.View,
                    viewDictionary,
                    new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
                    sw,
                    new HtmlHelperOptions()
                );

                await viewResult.View.RenderAsync(viewContext);
                return sw.ToString();
            }
        }

and a lot of views, which start from the path: my class library root/Views//Shared/many views.

The problem is, that IRazorViewEngine can't find my views, how should I call viewRenderService.RenderToStringAsync(?) to render ~/Views/Shared/Myview.cshtml, for example?


Solution

  • The way I handle views in class libraries is to make the views embedded resources, ie in the .csproj file I have

    <ItemGroup>
      <EmbeddedResource Include="Views\**" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
    </ItemGroup>
    

    and you need this package:

    <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="1.1.*" />
    

    and then I have an extension method in my class library like this:

    public static RazorViewEngineOptions AddCloudscribeSimpleContentBootstrap3Views(this RazorViewEngineOptions options)
    {
        options.FileProviders.Add(new EmbeddedFileProvider(
            typeof(Bootstrap3).GetTypeInfo().Assembly,
            "cloudscribe.SimpleContent.Web.Views.Bootstrap3"
        ));
    
        return options;
    }
    

    and then in Startup.cs of the main app you have to opt in to including those views using the extension method like this:

    services.AddMvc()
    .AddRazorOptions(options =>
    {
        options.AddCloudscribeSimpleContentBootstrap3Views();
    });