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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fabio Andrés
  • 229
  • 2
  • 11
  • 1
    Have you tried something like this? `services.AddControllersWithViews().AddRazorRuntimeCompilation(options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));` – DavidG Oct 08 '19 at 15:28
  • Muchas Gracias DavidG. Thanks very Much DavidG. – Fabio Andrés Oct 08 '19 at 15:38
  • RazorViewEngineOptions for FileProviders in ASP.NET Core 3.0 #14593 https://github.com/aspnet/AspNetCore.Docs/issues/14593 – Fabio Andrés Oct 08 '19 at 15:45

2 Answers2

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)));
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    This is awesome, as it also works in [Razor Class Libraries (RCL)](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.1&tabs=visual-studio) and even in [JetBrains Rider](https://www.jetbrains.com/rider/). – Uwe Keim Oct 27 '20 at 12:54
  • Requires adding nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation – Stefan Steiger Nov 29 '20 at 20:59
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

Fabio Andrés
  • 229
  • 2
  • 11