0

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 i.e. SimpleProductPage.cshtml and this View will have it's Controls i.e. 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.

Deepak
  • 428
  • 5
  • 12
  • MVC.net uses naming convention to load views so that it is entirely clear when visiting a page on the front end where those views come from without the developer having to hunt through code to find the logic used to load the view. What you suggest is circumventing this feature, so you might want to consider that before implementation. But yes, you can implement a custom [RazorViewEngine](http://stackoverflow.com/questions/9838766/implementing-a-custom-razorviewengine) to do this – CodingIntrigue Mar 05 '14 at 08:47
  • 1
    You can give a location of a view to loaded like return View("~/folder/viewname.cshtml") or create your own `Editor` templates. You can make use of `MVC Areas` to keep the folder structure – Murali Murugesan Mar 05 '14 at 09:47
  • @Murali I will try to do the Folder based Location Parameter. Can you suggest how Areas can help, as I thought Areas can be used for Separation of Application like /Admin or /Support, I hope I made things clear. – Deepak Mar 05 '14 at 10:15

2 Answers2

1

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/

ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • 1
    Please provide relevant code in your answers where possible. Links to external websites aren't likely to stay alive to help anyone with the same problem in the future – CodingIntrigue Mar 05 '14 at 08:49
  • @RGraham,@ps2goat I don't know if I can Create the View Engine efficiently, as I am new to MVC, but I will try for it. – Deepak Mar 05 '14 at 10:14
  • @RGraham, that's true, but I'm not going to plagiarize someone's code if a lot of effort was put into it with great detail. – ps2goat Mar 05 '14 at 16:22
  • 1
    It's not plagarism with attribution. Post the code *and* the link – CodingIntrigue Mar 05 '14 at 20:31
0

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.

Deepak
  • 428
  • 5
  • 12