简体   繁体   中英

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

Names are folders and FileNames are specified with Extentions.

Website Folder Structure:

  • Root
    • DisplayControls
      • Product
        • SimpleProductPage
          • SimpleProductPage.cshtml
          • Controls
            • Image.cshtml
            • ProductSpecification.cstml
        • ProductPageWithImageGallery
          • ProductPageWithImageGallery.cshtml
          • Controls
            • ImageGallery.cshtml
            • ProductSpecification.cstml
      • Category
        • SimpleCategoryPage
          • SimpleCategoryPage.cshtml
          • Controls
            • CateogryProducts.cshtml
            • RelatedProducts.cshtml
        • CategoryPage
          • CategoryPage.cshtml
          • Controls
            • CateogryProducts.cshtml
            • RelatedProducts.cshtml
    • Controllers
      • someControllerfiles.cs
    • Views
      • ProductView.cshtml
      • CategoryView.cshtml
      • SomeotherViews......
    • 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. 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. and if the Selected Display Control is "ProductPageWithImageGallery" then the Views from that Folder will be Loaded along with its child/Partial Views.

Same goes for Category Pages, based on the DB Entries, Respective Controls/Biews will be Loaded from that Folder.

So basically ProductView.cshtml and CategoryView.cshtml will act as Placeholder and the Selected Views/ChildViews will be loaded onto them runtime. 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.

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.

One blog on the subject:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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