在 Razor (chtml) 中渲染动态视图,如何在 asp.net core 3.0 中将 FileProvider 添加到 razor?
Posted
技术标签:
【中文标题】在 Razor (chtml) 中渲染动态视图,如何在 asp.net core 3.0 中将 FileProvider 添加到 razor?【英文标题】:Rendering dynamics views in Razor (chtml), How to add a FileProvider to razor in asp.net core 3.0? 【发布时间】:2019-10-08 15:16:12 【问题描述】:我正在从 asp-net core 2.2 迁移到 asp-net core 3.0,我使用这个块来定义我的文件类提供程序,你知道这是用来创建动态剃须刀视图(动态 cshtml),我的问题是该段不起作用。
我已经实现了经典:
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(
new MyCostumizedFileProvider()
)
);
地点:
MyCostumizedFileProvider:是File Provider的实现。
RazorViewEngineOptions :是 aspnet core 2.0 中使用的 Razor 运行时编译器的处理程序。
在 asp-net core 中我已经检查过
例如:
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)
你有线索,解决方案吗?或者你知道注入 FileProviders 的其他方法吗?
【问题讨论】:
你试过这样的事情吗?services.AddControllersWithViews().AddRazorRuntimeCompilation(options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));
Muchas Gracias DavidG.非常感谢 DavidG。
RazorViewEngineOptions for FileProviders in ASP.NET Core 3.0 #14593 github.com/aspnet/AspNetCore.Docs/issues/14593
【参考方案1】:
对于 ASP.NET MVC Core 3,这是添加文件提供程序的方法:
services.AddControllersWithViews()
.AddRazorRuntimeCompilation(options => options.FileProviders.Add(
new PhysicalFileProvider(appDirectory)));
【讨论】:
这太棒了,因为它也适用于Razor Class Libraries (RCL) 甚至JetBrains Rider。 需要添加 nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation【参考方案2】:DavidG 为我们提供了解决方案:(我已经测试过,它有效)。
解决办法是:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));
在这种情况下:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new MyCostumizedFileProvider()));
【讨论】:
你应该让评论的人实际添加答案。以上是关于在 Razor (chtml) 中渲染动态视图,如何在 asp.net core 3.0 中将 FileProvider 添加到 razor?的主要内容,如果未能解决你的问题,请参考以下文章
使用 MVC 渲染具有嵌入式 Razor 变量的动态 HTML