问题
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:
MyCostumizedFileProvider : is the Implementation of File Provider.
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