繁体   English   中英

ASP.NET MVC从不同的文件夹动态加载视图/局部视图

[英]ASP.NET MVC Load Views / Partial Views Dynamically from Different Folder

对于在实现以下方案的技术/策略方面的任何帮助,我将不胜感激:

我们需要ASP.NET MVC 5网站的这种文件夹结构

名称是文件夹,文件名使用扩展名指定。

网站文件夹结构:


    • DisplayControls
      • 产品
        • SimpleProductPage
          • SimpleProductPage.cshtml
          • 控制
            • Image.cshtml
            • ProductSpecification.cstml
        • ProductPageWithImageGallery
          • ProductPageWithImageGallery.cshtml
          • 控制
            • ImageGallery.cshtml
            • ProductSpecification.cstml
      • 类别
        • SimpleCategoryPage
          • SimpleCategoryPage.cshtml
          • 控制
            • CateogryProducts.cshtml
            • RelatedProducts.cshtml
        • CategoryPage
          • CategoryPage.cshtml
          • 控制
            • CateogryProducts.cshtml
            • RelatedProducts.cshtml
    • 控制器
      • someControllerfiles.cs
    • 查看
      • ProductView.cshtml
      • CategoryView.cshtml
      • SomeotherViews ......
    • OtherFilesAndFolders

现在,如果我们要请求“产品页面”,则将从控制器中加载ProductView.cshtml,并在该视图中,基于数据库条目从“ DisplayControls”文件夹中加载一些显示控件。 如果条目说我们需要加载“ SimpleProductPage”,那么我们将从“ SimpleProductPage”文件夹即SimpleProductPage.cshtml加载视图,并且该视图将具有控件,即从控件文件夹加载部分视图。 如果“选定的显示控件”为“ ProductPageWithImageGallery”,则该文件夹中的视图将与其子视图/部分视图一起加载。

类别页也是如此,根据数据库条目,将从该文件夹中加载相应的控件/ Biew。

因此,基本上ProductView.cshtml和CategoryView.cshtml将充当占位符,并且所选视图/ ChildView将在运行时加载到它们。 如您所见,我们的目标视图未位于“视图”文件夹中,因此对我来说有点问题。

我是MVC的新手,我们正在使用MVC 5,请您帮助我如何实现此功能。

您需要创建一个自定义视图引擎(最有可能继承自Razor视图引擎),并覆盖一个确定在哪里寻找视图的方法。

关于这个主题的一个博客:

http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/

我处理上述情况的方式是创建一个自定义视图引擎,该引擎将从指定的文件夹结构中加载视图。

希望这对某人有帮助,

谢谢大家的支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM