leora
leora

Reputation: 196459

In asp.net-mvc, what is the best way to deal with a crowded Shared View folder?

I have a large ASP.NET-MVC project and I have a lot of files in the shared View folder.

I want to group these files (Views) to make it easier to understand. I saw this blog post which seems to have the same issue but I would like to avoid an additional dependencies / dll in my solution.

Is there anyway to organize the Shared Views directory without any external dependencies. I am using the aspx View Engine (NOT Razor)

Upvotes: 3

Views: 1076

Answers (6)

Dave Alperovich
Dave Alperovich

Reputation: 32500

In ASP.NET MVC, sharing works in multiple ways.

1) Use the Controller's View Folder.

Yes, View Engine will always check you Shared folder for a partial view. But before checking the shared folder, the View Engine will check the "active" folder.

So to explain in better detail, take a scenario where you are using HomeController, rendering a View in the "Home" View folder, and you are rendering a Partial View called "NavPartial". Before checking your "Shared" folder for the Partial View, the View Engine will check your "Home" View folder first.

This means that if you need to re-use Partials within a Controller, you can pack them into that Controller's dedicated View Folder.

I find this approach convenient. I organize my project and Controller structure to take advantage.

2) Use Areas

If you use Areas, each Area has a dedicated "Shared" folder. This is again convenient if your functionality is local to that Area.

Upvotes: 4

Tomaz Tekavec
Tomaz Tekavec

Reputation: 764

As the link you provided refers to MVC 3, I created an example for this version. Assuming the example will contain a subfolder named 'Group1', you can create a custom view engine class and add a custom path to the PartialViewLocationFormats collection:

public class MyViewEngine : WebFormViewEngine   
{
    public MyViewEngine()
    {
        PartialViewLocationFormats = PartialViewLocationFormats.Union(new[]
            {
                "~/Views/Shared/Group1/{0}.ascx"
            }).ToArray();
    }
}

You can of course add additional paths if you have several subfolders in the 'Views/Shared' folder.

Edit the Global.asax file and add MyViewEngine to the ViewEngines.Engines collection in the Application_Start() method:

    protected void Application_Start()
    {
        ViewEngines.Engines.Add(new MyViewEngine()); 
        //the remaining Application_Start implementation follows below
        .....
    }

In the sample application I've added two partial views:

  • SharedRoot.ascx is located in the 'Views/Shared' folder
  • SharedGroup1.ascx is located in the 'Shared/Group1' folder

A sample Index.ascx file would then look like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: ViewBag.Message %></h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<p>
    <%: Html.Partial("SharedRoot") %>
</p>
<p>
    <%: Html.Partial("SharedGroup1") %>
</p>

Here is the picture of the sample solution structure:

MVC - partial views in subfolders

I've also uploaded a sample VS 2012 MVC 3 application:

http://www.biromt.com/samples/MvcSharedViewsInSubfolders.zip

UPDATE: Please note you can't use files with the same name in different subfolders with this solution. You will always get only the first partial view (according to the search order) while others will be ignored. So, if you just have to arrange files in the Shared folder among subfolders, this concept is OK. If you add files afterwards or rename some of them, keep this note in mind. See the discussion beneath for more details (thanks, Max).

Upvotes: 3

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

If you are changing regular hierarchy then you have just to tell the new folder hierarchy like if I created a Group for shared views:

group

then to access a partial view just write:

return PartialView("Group1/_Chart", model);

Upvotes: 0

Max
Max

Reputation: 3328

Inside the Views folder, Create any folder structure you find works logically for you.

Then just call each view (or partial) using the full relative path to the file. Even if its more typing calling the views, with a good folder structure it will save time as it will be easier and much clearer to see what kind of (logically) view you are calling.

This can also have some performance benefits as view rendering won't need to go look for your views in several possible locations.

I find it very rare benefit in doing it the "convention based way" if it doesn't fit your needs.


I might also mention another good tip; remove all default view engines and make sure only the one you are using is registered. This will increase performance as well. Like this:

protected void Application_Start() {
    ViewEngines.Engines.Clear();
    // ViewEngines.Engines.Add(new RazorViewEngine());
    ViewEngines.Engines.Add(new WebFormViewEngine());
}

Upvotes: 1

Amirhossein Mehrvarzi
Amirhossein Mehrvarzi

Reputation: 18954

Just Follow this tips:

  1. Create each PartialView into its related Controller Folder to avoid crowded Shared Folder.
  2. Use your own Naming Convention for Shared items, so cluster each subject by using related characters at the beginning of its name. And when you Crawl Shared Folder, just simply press related key to find its group!
  3. If you deal with really separate concepts in your project, use Areas. That is simple way and easy to configure too. Each Area allows you to have separate Shared Folder on it.

Please Don't try to change the logic of Microsoft Platform. They know this trouble better than us. According to my experience, Changing the logic of MVC application in each manner can cause to appear ambiguous errors or functionality. Perhaps an Update to enhance this part be in way.

Upvotes: 0

Vishal Sharma
Vishal Sharma

Reputation: 2803

that is for what for asp.net mvc is , all can be customizable.

you can do things like this where your structure looks like

Folder Structure

this is about displaying templates

to display use

@Html.Display("","Home/Managable",null)

Display is helper extension method that find the partial view and render..

if you want editor based then you can use Editor Templates and Extenstion method is Editor , EditorFor etc..

Example here is in Razor but same applies to aspx view engine also..

Upvotes: 3

Related Questions