在 Razor (chtml) 中渲染动态视图, 如何在 Core 3.0 中将文件提供程序添加到 razor asp.



我正在从asp-net core 2.2迁移到asp-net core 3.0,我正在使用这个块来定义我的文件类提供程序,如您所知,这是用于创建动态剃刀视图(动态cshtml(,我的问题是该段不起作用。

我已经实现了经典:

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

哪里:

  1. MyCostumizedFileProvider:是文件提供程序的实现。

  2. RazorViewEngineOptions:是aspnet core 2.0中使用的Razor运行时编译器的处理程序。

在 asp-net 核心中,我已经检查过

例如:

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

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

错误始终为:

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

你有线索,解决方案吗?或者你知道注入文件提供程序的其他方法吗?

对于 ASP.NET MVC Core 3,这是添加文件提供程序的方法:

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

DavidG为我们提供了解决方案:(我已经测试过并且它可以工作(。

解决方案是:

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

在这种情况下:

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

确保添加此库:Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

相关内容

  • 没有找到相关文章

最新更新