我知道在Asp.Net中可以为Global.asax中的部分视图设置默认位置格式,并且我知道在Asp.Net Core中可以为类似于以下视图的视图设置ViewLocationFormats:
// services is of type IServiceCollection
services.AddMvc(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorOptions(options =>
{
// {0} - Action Name
// {1} - Controller Name
// {2} - Area Name
// Replace normal view entirely
options.ViewLocationFormats.Clear();
options.ViewLocationFormats.Add("/Features/{1}/{0}.cshtml"); //Features/{Controller Name}/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/{1}/Views/{0}.cshtml"); //Features/{Controller Name}/Views/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/Shared/{0}.cshtml"); //Features/Shared/{Action Name}.cshtml
options.ViewLocationFormats.Add("/Features/Shared/Views/{0}.cshtml"); //Features/Shared/Views/{Action Name}.cshtml
});
Run Code Online (Sandbox Code Playgroud)
对于我的一生,我不知道如何设置局部视图的位置格式?
由于Razor引擎似乎无法找到部分视图,因此以下代码部分将无法工作:
public async Task<PartialViewResult> EntityChangeDetailModal(EntityChangeListDto entityChangeListDto)
{
var output = await _auditLogAppService.GetEntityPropertyChanges(entityChangeListDto.Id);
var viewModel = new EntityChangeDetailModalViewModel(output, entityChangeListDto);
return PartialView("_EntityChangeDetailModal", viewModel);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以为部分视图显式传递相对路径,但是我希望能够仅更新默认位置,以便Razor引擎可以解析这些引用,而无需我对现有代码库进行大量更改。
请在startup.cs中添加此代码以配置默认的部分视图
可以使用启动中的ConfigureServices方法中的Razor视图引擎选项将其他位置添加到默认注册的搜索路径(页面/共享和视图/共享)。以下代码块将 Pages/Partials 文件夹添加到搜索路径,这意味着您可以将部分文件放在那里并找到它们:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddRazorOptions(options =>
{
options.PageViewLocationFormats.Add("/Pages/Partials/{0}.cshtml");
});
}
Run Code Online (Sandbox Code Playgroud)
MVC Global.asax 中的customEngine.PartialViewLocationFormats
ViewEngines.Engines.Clear();
var customEngine = new RazorViewEngine();
customEngine.PartialViewLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Partial/{0}.cshtml",
"~/Views/Partial/{1}/{0}.cshtml"
};
ViewEngines.Engines.Add(customEngine);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |