在 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?
我正在从 asp-net core 2.2 迁移到 asp-net core 3.0,我使用这个块来定义我的文件 Class Provider,如你所知这是用于创建动态剃刀视图(动态 cshtml),我的问题是该段不起作用。
我实现了经典:
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(
new MyCostumizedFileProvider()
)
);
其中:
MyCostumizedFileProvider : 是文件提供程序的实现。
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)
你有解决办法吗?或者你知道其他注入 FileProvider 的方法吗?
DavidG 为我们提供了解决方案:(我已经测试过并且有效)。
解决方法是:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));
在这种情况下:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new MyCostumizedFileProvider()));
对于ASP.NET MVC Core 3,这是添加文件提供程序的方法:
services.AddControllersWithViews()
.AddRazorRuntimeCompilation(options => options.FileProviders.Add(
new PhysicalFileProvider(appDirectory)));
我正在从 asp-net core 2.2 迁移到 asp-net core 3.0,我使用这个块来定义我的文件 Class Provider,如你所知这是用于创建动态剃刀视图(动态 cshtml),我的问题是该段不起作用。
我实现了经典:
services.Configure<RazorViewEngineOptions>(opts =>
opts.FileProviders.Add(
new MyCostumizedFileProvider()
)
);
其中:
MyCostumizedFileProvider : 是文件提供程序的实现。
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)
你有解决办法吗?或者你知道其他注入 FileProvider 的方法吗?
DavidG 为我们提供了解决方案:(我已经测试过并且有效)。
解决方法是:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new PhysicalFileProvider(appDirectory)));
在这种情况下:
services.AddControllersWithViews().AddRazorRuntimeCompilation(
options => options.FileProviders.Add(new MyCostumizedFileProvider()));
对于ASP.NET MVC Core 3,这是添加文件提供程序的方法:
services.AddControllersWithViews()
.AddRazorRuntimeCompilation(options => options.FileProviders.Add(
new PhysicalFileProvider(appDirectory)));