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.

3
  • 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 to do this Commented Mar 5, 2014 at 8: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 Commented Mar 5, 2014 at 9: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. Commented Mar 5, 2014 at 10:15

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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.
@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.
It's not plagarism with attribution. Post the code and the link
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.