简体   繁体   中英

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

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?

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

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM