Rendering dynamics views in Razor (chtml), How to add a FileProvider to razor in asp.net core 3.0?

三世轮回 提交于 2020-12-26 22:51:22

问题


I am migrating from asp-net core 2.2 to asp-net core 3.0, I was using this block to define my File Class Provider, as you know this is used to create dynamic razor views (dynamic cshtml), my problem is that segment does not work.

I have implemented the classic:

services.Configure<RazorViewEngineOptions>(opts =>
                opts.FileProviders.Add( 
                    new MyCostumizedFileProvider()
                )
            );

Where:

  1. MyCostumizedFileProvider : is the Implementation of File Provider.

  2. RazorViewEngineOptions : is the handler of the Razor runtime compilator used in aspnet core 2.0.

In asp-net core I have checked

Example like:

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
    options.FileProviders.Clear();
    options.FileProviders.Add(new MyCostumizedFileProvider());
});

or

  services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
   options.FileProviders.Add(new MyCostumizedFileProvider());
  });

The error is always:

/Pages/Shared/ViewListPerson_1b2b2019-aad9-4096-a3b7-ece5dfe586c1.cshtml
   at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
   at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Do you have a clue, solution? or Do you know other way to inject FileProviders?


回答1:


For ASP.NET MVC Core 3, this is the way to add a file provider:

services.AddControllersWithViews()
    .AddRazorRuntimeCompilation(options => options.FileProviders.Add(
        new PhysicalFileProvider(appDirectory)));



回答2:


DavidG provided us the solution: (I have tested and it works).

The solution is:

services.AddControllersWithViews().AddRazorRuntimeCompilation(
    options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));

In this case:

services.AddControllersWithViews().AddRazorRuntimeCompilation(
    options => options.FileProviders.Add(new MyCostumizedFileProvider()));


来源:https://stackoverflow.com/questions/58289323/rendering-dynamics-views-in-razor-chtml-how-to-add-a-fileprovider-to-razor-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!