In this tip, I demonstrate how you can retrieve a view from any folder in an ASP.NET MVC application. I show you how to use both specific paths and relative paths.

Until today, I thought that a controller action could return a view from only one of two places:

· Views\controller name

· Views\Shared

For example, if you are working with the ProductController, then I believed that you could only return a view from either the Views\Product folder or the Views\Shared folder. When looking through the source code for the ViewLocator class, I discovered that I was wrong. If you supply a “Specific Path” for a view, you can retrieve a view from any location in an ASP.NET MVC application.

The ProductController.Index() action in Listing 1 returns a view from the specific path ~\Confusing\ButWorks.aspx.

Listing 1 – ProductController.vb (VB.NET)

Public Class ProductController
    Inherits Controller
 
      Public Function Index() As ActionResult
       Return View("~\Confusing\ButWorks.aspx")
      End Function
End Class

Listing 1 – ProductController.cs (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace Tip24.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            return View( @"~\Confusing\ButWorks.aspx");
        }
    }
}
 

A specific path is a path that starts with either the character ~ or /. Any other path gets treated differently.

You also can use relative paths such as SubProduct\Details or SubProduct/Details. Either relative path will return a view located at Views\Product\SubProduct\Details.aspx. Listing 2 contains a complete code listing that illustrates using relative paths.

Listing 2 – ProductController.vb (VB.NET)

Public Class ProductController
    Inherits Controller
  
      Public Function Index() As ActionResult
       Return View("SubProduct\Details")
      End Function
 
End Class

Listing 2 – ProductController.cs (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace Tip24.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            return View(@"SubProduct\Details");
        }
 
    }
}

Now, I want to be the first to warn you that you should never, never, never use this tip (Please delete this entry from your news aggregator immediately). There is a good reason for following the conventions inherent in an MVC application. Placing your files in known locations makes it easier for everyone to understand your application.

If you liked this blog post then please Subscribe to this blog.
posted on Wednesday, July 23, 2008 5:20 PM | Filed Under [ ASP.NET ASP.NET MVC Tips ]

Comments

Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by http://
on 7/23/2008 6:53 PM

Good tip. Agreed on the 'never break this rule' bit. Except I break it. My User, Products, etc controllers all, er, control their own Admin actions rather than making a different AdminUser, AdminProducts, etc controller, so I do sub-folder those views. So rather than having a index.aspx and an admin_index.aspx in the same folder. I have an index.aspx and an admin/index.aspx, and I use View("admin/index") in the admin_index() action. Breaking the rules? Sure, a little, but it's still crystal clear.
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Scordi
on 7/23/2008 8:07 PM

Hi,<br /><br />yesterday I did a post on my Blog in german language to show how to implement a custom viewlocator which only defines another target folder for the views. In my example its '/MVC/Views'.<br /><br />Maybe you want to complete your blog post by mentioning this approach.<br /><br />Here is my blogpost:<br /><br /><a rel="nofollow external" href="http://blog.dotnet-expert.de/2008/07/22/ImplementierenEinesEigenenViewLocatorsF%c3%bcrASPNetMVC.aspx" title="http://blog.dotnet-expert.de/2008/07/22/ImplementierenEinesEigenenViewLocatorsF%c3%bcrASPNetMVC.aspx">blog.dotnet-expert.de/.../...%c3%bcrASPNetMVC.aspx</a><br /><br />Greet,<br />Jens
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Eric Hexter
on 7/25/2008 4:10 AM

Thanks for stressing the convention. I think it is really important have this been an extreme case for drifting from the convention.
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Merritt
on 8/25/2008 8:01 PM

I had to include 'Views' in the path to get this to work:<br /><br />@"~\Views\Confusing\ButWorks.aspx"
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by http://
on 9/24/2008 8:32 PM

I landed on this post because I'm trying to do what Jens is doing - it would be nice to have a configurable root directory, rather than assuming the root of the web site.<br /><br />For example, I am trying to have the MVC run under an /admin/ directory, but nowhere else in the web site.<br /><br />Jens' solution actually is pretty good! Thanks for the discussion!
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Jason Monroe
on 4/30/2009 12:42 PM

@Stephen<br /><br />I know that I'm late to the party with MVC and I'm digging up a 9 month post.. but this one is relevant to something I was trying to solve :)<br /><br />I wanted to organize all of my "Admin" views into a ~/Views/Admin/<Function>/<Action>.aspx scheme so that the root of my views folder wouldn't be cluttered with folders of views that are not relevant to the bulk of the site, and once written are rarely if ever touched.<br /><br />My solution was to write a new base class for the administration controllers (inherited from controller) and building up the viewName from the base.<br /><br />I made a post about it here: <a rel="nofollow external" href="http://www.clanmonroe.com/Blog/archive/2009/04/30/organize-your-views-in-asp.net-mvc.aspx" title="http://www.clanmonroe.com/Blog/archive/2009/04/30/organize-your-views-in-asp.net-mvc.aspx">www.clanmonroe.com/.../...iews-in-asp.net-mvc.aspx</a><br /><br />What's prompted me to comment on your post (aside from a <nod thanksReason="inspiration" />) was your comment about "I want to be the first to warn you that you should never, never, never use this tip".<br /><br />I would like to add this food for thought: "Part of being a good developer is following the rules... Part of being a great developer is knowing when to break them."
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Len Ocin
on 5/26/2009 5:24 AM

Why do you write up advice and then put in a footnote (which nobody will ever read until too late) they should not follow your advice?<br /><br />Sh!t, you made me do it, and now I'm stuck on the POST implementation of a View controller, which you and your stupid advice never thought of.
Gravatar
# re: ASP.NET MVC Tip #24 – Retrieve Views from Different Folders
Posted by Gary Kellett
on 7/25/2009 4:24 AM

I agree it is always better to "follow the rules", but it is good to know when/how to break them.<br /><br />For example, in the application I am writing, I have seperated out a number of controllers into various sections - lets take "Sales" for example. I have a CustomersController, ProjectsController and SalesInvoicingController. It would seem logically incorrect to put all of these actions within the same controller.<br /><br />Now in the same pattern, I group my views under a "Sales" folder, so a subfolder for "Customers", one for "Projects" and one for "SalesInvoicing".<br /><br />Mvc should really pick these up as they match the name and hierarchy of the controllers exactly, but no - I have to break the rules to return a specific view for each action in the controller. I am sure there must be a better way than hard-coding in the view paths.......<br />
Comments have been closed on this topic.