简体   繁体   English

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

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

I would appreciate any assistance with techniques/ strategies to implement the scenario below: 对于在实现以下方案的技术/策略方面的任何帮助,我将不胜感激:

We need to have this kind of Folder structure of our ASP.NET MVC 5 site 我们需要ASP.NET MVC 5网站的这种文件夹结构

Names are folders and FileNames are specified with Extentions. 名称是文件夹,文件名使用扩展名指定。

Website Folder Structure: 网站文件夹结构:

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

Now If we want to request the Product Page then ProductView.cshtml will be Loaded from Controller and Inside that View we want to Load Some Display Control from "DisplayControls" Folder based on Database Entry. 现在,如果我们要请求“产品页面”,则将从控制器中加载ProductView.cshtml,并在该视图中,基于数据库条目从“ DisplayControls”文件夹中加载一些显示控件。 If the Entry says we need to Load "SimpleProductPage" Then We will Load the Views from "SimpleProductPage" Folder ie SimpleProductPage.cshtml and this View will have it's Controls ie Partial Views Loaded from it's Controls Folder. 如果条目说我们需要加载“ SimpleProductPage”,那么我们将从“ SimpleProductPage”文件夹即SimpleProductPage.cshtml加载视图,并且该视图将具有控件,即从控件文件夹加载部分视图。 and if the Selected Display Control is "ProductPageWithImageGallery" then the Views from that Folder will be Loaded along with its child/Partial Views. 如果“选定的显示控件”为“ ProductPageWithImageGallery”,则该文件夹中的视图将与其子视图/部分视图一起加载。

Same goes for Category Pages, based on the DB Entries, Respective Controls/Biews will be Loaded from that Folder. 类别页也是如此,根据数据库条目,将从该文件夹中加载相应的控件/ Biew。

So basically ProductView.cshtml and CategoryView.cshtml will act as Placeholder and the Selected Views/ChildViews will be loaded onto them runtime. 因此,基本上ProductView.cshtml和CategoryView.cshtml将充当占位符,并且所选视图/ ChildView将在运行时加载到它们。 As you can see our targeted Views are not located into Views Folder so that is a bit of problem for me. 如您所见,我们的目标视图未位于“视图”文件夹中,因此对我来说有点问题。

I am new to MVC and we are using MVC 5, Can you people please help me how to implement this. 我是MVC的新手,我们正在使用MVC 5,请您帮助我如何实现此功能。

You'll need to create a custom view engine, most likely inheriting from the Razor view engine, and override a method that determines where to look for views. 您需要创建一个自定义视图引擎(最有可能继承自Razor视图引擎),并覆盖一个确定在哪里寻找视图的方法。

One blog on the subject: 关于这个主题的一个博客:

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

the way I handled above situation is by creating a custom view engine, which will load views from the specified folder structure. 我处理上述情况的方式是创建一个自定义视图引擎,该引擎将从指定的文件夹结构中加载视图。

Hope this helps to someone, 希望这对某人有帮助,

Thank you all for your support. 谢谢大家的支持。

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

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