Asp.Net Core 2.1:设置局部视图的默认位置格式

时间:2018-08-02 15:05:50

标签: asp.net-mvc razor

我知道在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                
        });

在我生命中,我不知道如何设置局部视图的位置格式?

以下代码部分将无法使用,因为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);
    }

我知道我可以显式传递局部视图的相对路径,但是我希望能够仅更新默认位置,以便Razor引擎可以解析这些引用,而无需我对我们的外观进行大量更改。现有的代码库。

2 个答案:

答案 0 :(得分:0)

请将此代码添加到startup.cs中以配置默认的部分视图

可以使用“启动”中的ConfigureServices方法中的Razor视图引擎选项将其他位置添加到默认注册的搜索路径(“页面/共享”和“视图/共享”)。以下代码块将Pages / Partials文件夹添加到搜索路径,这意味着您可以在其中放置部分文件并找到它们:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddRazorOptions(options =>
    {
        options.PageViewLocationFormats.Add("/Pages/Partials/{0}.cshtml");
    });
}
用于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);

答案 1 :(得分:0)

您可以使用此:

    public class ViewLocationExpander : IViewLocationExpander
    {

        /// <summary>
        /// Used to specify the locations that the view engine should search to 
        /// locate views.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="viewLocations"></param>
        /// <returns></returns>
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            //{2} is area, {1} is controller,{0} is the action
            var dlllName = Assembly.GetAssembly(GetType()).GetName().Name;
            string[] locations = new string[] {
                $"/Extentions/{dlllName}/Areas/{{2}}/Views/        public void Execute(IServiceCollection serviceCollection, IServiceProvider serviceProvider)
        {
            serviceCollection.Configure<RazorViewEngineOptions>(options => {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });
        }

/enter image description here.cshtml",
                $"/Extentions/{dlllName}/Areas/{{2}}/Views/{{0}}.cshtml",
                $"/Extentions/{dlllName}/Views/{{2}}/def J(t, f, a, b, J, H, N, T):

   J = (M(t, f, a, b, J, H, N, T) * np.sin(t))/(4*np.pi)

   return J
/{{0}}.cshtml",
                $"/Extentions/{dlllName}/Views/{{0}}.cshtml",
            };
            return locations.Union(viewLocations);          //Add mvc default locations after ours
        }


        public void PopulateValues(ViewLocationExpanderContext context)
        {
            context.Values["customviewlocation"] = nameof(ViewLocationExpander);
        }
    }

然后在启动类ConfigureServices中:

<script>
$(document).ready(function() {
   $('#second').change();
   //or
   $('#second').trigger('change');
});
</script>