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

2

6

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?

Jesusitajet answered 8/10, 2019 at 15:16 Comment(3)
Have you tried something like this? services.AddControllersWithViews().AddRazorRuntimeCompilation(options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));Lynnalynne
Muchas Gracias DavidG. Thanks very Much DavidG.Synod
RazorViewEngineOptions for FileProviders in ASP.NET Core 3.0 #14593 github.com/aspnet/AspNetCore.Docs/issues/14593Synod
L
10

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)));
Lynnalynne answered 8/10, 2019 at 15:47 Comment(2)
This is awesome, as it also works in Razor Class Libraries (RCL) and even in JetBrains Rider.Fanelli
Requires adding nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilationRolandrolanda
J
1

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

Make sure to add this library: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

Jesusitajet answered 8/10, 2019 at 15:43 Comment(2)
You should let the person who commented actually add the answer.Lynnalynne
This solution worked after adding Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project.Kokura

© 2022 - 2024 — McMap. All rights reserved.