Location Formats for ASP.NET MVC

ASP.NET MVC is a wonderful thing.  One of the many great features is the ability to customize all of the .NET framework's code by swapping out one implementation and using another.  One such instance is creating a custom view engine, which you can do as illustrated in this example: http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx.  The point of my article is not to illustrate how this can be done, but about how to customize it for your needs.  By default, the web forms view engine looks for views in the folder of the controller or the shared folder.  So if you try to trigger an action method "Index" within the controller of type CustomerController, a partial view (.ascx) or the view (.aspx) is sought for in the ~/Shared folder or ~/Customer folder.

Now, I tend to like to use partial views in order to separate and reuse functionality a lot.  So I tend to have a lot of partial views that tend to get reused across pages and I don't want everything to be in the shared folder (by default, partial views have to be in the shared folder or in the same folder as the controller).  So I added some code to the view engine that allowed me to create subfolders within the shared folder and for the view engine to look for the classes there.  Imagine this folder structure:

Shared
    Customers
    Orders
    Products

So the shared folder breaks up my partial views into the folder above. 

Thinking long-term, rather than hard-coding all these folder references and assigning them to the ViewLocationFormats and PartialViewLocationFormats properties, I wanted something that I wouldn't have to worry about changing later.  So in true ASP.NET MVC framework form, I created some extra code to create the ability to automatically add references to subfolders too.  In order to do this, it's required to use the VirtualPathProvider class to extract the URL, as in the following code:

public MyViewEngine() {
var locations = new List<string>
{
    "~/Views/{1}/{0}.aspx",
    "~/Views/{1}/{0}.ascx",
    "~/Views/Shared/{0}.aspx",
    "~/Views/Shared/{0}.ascx"
};

var dir = this.VirtualPathProvider.GetDirectory("~/Views/Shared");
var subs = dir.Directories.OfType<VirtualDirectory>();

foreach (var sub in subs)
{
    locations.Add("~" + sub.VirtualPath.Substring(sub.VirtualPath.IndexOf("/", 2)) + "{0}.ascx");
}

base.ViewLocationFormats = locations.ToArray();
base.PartialViewLocationFormats = base.ViewLocationFormats;
}

This is the constructor for the custom view engine.  It contains some additional code to use the VirtualPathProvider property (a property of our custom view engine) to extract the subdirectories of the shared folder.  You see the four hard-coded references at the beginning, and so we need to create virtual path strings (which start with "~" and work from the beginning of the virtual directory) to add to the custom list.  When working with folders using VirtualPathProvider, the issue becomes the way paths are referenced.  By default, the path may be:

/MyVirtualFolder/Views/Shared/Customers/

When you need:

~/Views/Shared/Customers/

And so some additional work to format the path is needed (the substring strips off the virtual directory folder.  Now we have a component that will allow the MVC framework to look for partial views in all subdirectories in the shared folder.

Published Monday, November 23, 2009 10:03 PM by bmains
Filed under: ,

Comments

# re: Location Formats for ASP.NET MVC

Thursday, February 18, 2010 9:44 AM by ukessays201

I agree using this language allow the developer to become flexible according to the needs of the company system.

# re: Location Formats for ASP.NET MVC

Friday, January 07, 2011 5:11 AM by suedallin

I tottaly agree with this.Thanks.

# re: Location Formats for ASP.NET MVC

Wednesday, January 19, 2011 8:03 AM by fastfinder

I am not sure if you know that the information you have shared above is unique and if you Google it, you would not find any similar! Thus such posts are very useful for many readers as I am! May I ask you, how do you choose topic for your writings and where do you get info’s for it? Thanks in advance.

# re: Location Formats for ASP.NET MVC

Wednesday, January 19, 2011 8:04 AM by fastfinder

I am not sure if you know that the information you have shared above is unique and if you Google it, you would not find any similar! Thus such posts are very useful for many readers as I am! May I ask you, how do you choose topic for your writings and where do you get info’s for it? Thanks in advance.

# re: Location Formats for ASP.NET MVC

Monday, January 24, 2011 2:21 PM by roxxky

This post shows the information which is close to standard. Hope next You will again post a nice Article/Information.

# re: Location Formats for ASP.NET MVC

Monday, January 24, 2011 2:22 PM by roxxky

hi

# re: Location Formats for ASP.NET MVC

Tuesday, February 08, 2011 5:31 AM by citizen titanium

This is a great source for those who is using ASP.net when looking for support. Hope you post more debugging and error codes and it's solution. <a href="www.citizentitanium.com/">citizen titanium</a>

# re: Location Formats for ASP.NET MVC

Thursday, March 03, 2011 9:14 AM by SaraFord

This is a great article and I'm pretty sure that it is worth of <a href="www.professay.com essays</a> writing.

# re: Location Formats for ASP.NET MVC

Sunday, March 13, 2011 1:11 PM by carlosr

I'm only recently began to get exposed to ASP.NET MVC (so yes, I didn't even hear about it until a couple of weeks ago) and I totally agree with the claim raised here that ASP.NET MVC is a wonderful thing.

# re: Location Formats for ASP.NET MVC

Thursday, April 21, 2011 5:07 AM by deesydia

To be honest, I don't know what ASP.NET is, but I'll save this page for reference, as my friend knows how to program using it.

Thanks for this article. :)

# re: Location Formats for ASP.NET MVC

Friday, May 06, 2011 9:54 AM by Susancai

What you share with us is very useful information! This is worth a read.

# re: Location Formats for ASP.NET MVC

Sunday, May 08, 2011 6:20 PM by psycho1111

Thanks for the codes.

Abir-<a href="http://www.the1waytostoppanicattacks.com" rel="follow">stop panic attacks</a>

# re: Location Formats for ASP.NET MVC

Thursday, May 12, 2011 9:59 AM by Tom_Goodman

It worked out that I

was able to use one or more of each of the essays I had written for each

college that I applied to.

<a href="www.bestessayhelp.com/.../buy-essay">essay writing service</a>

# re: Location Formats for ASP.NET MVC

Wednesday, June 01, 2011 4:54 AM by karlriggs31

"I'm only recently began to get exposed to ASP.NET MVC (so yes, I didn't even hear about it until a couple of weeks ago) and I totally agree with the claim raised here that ASP.NET MVC is a wonderful thing."

I definitely agree

# re: Location Formats for ASP.NET MVC

Wednesday, June 01, 2011 4:56 AM by karlriggs31

I definitely agree

# re: Location Formats for ASP.NET MVC

Monday, June 13, 2011 9:29 PM by HymanSanders

Thanks for ones marvelous posting! I certainly enjoyed reading it, you may be a great author.I will be sure to bookmark your blog and will eventually come back down the road. I want to encourage that you continue your great posts, have a nice morning!

# re: Location Formats for ASP.NET MVC

Monday, June 20, 2011 11:00 PM by harolddwell28

Nice post and I had a great time. Check out my african mango blog for my affiliate marketing site.  

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:26 PM by danleyvilla

Hello, I love reading through your post, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your efforts.

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:27 PM by danleyvilla

I like to take this chance to state that I really like your post. It has been a fine source of information for me in my study. Thank you so much.

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:27 PM by danleyvilla

I like to take this chance to state that I really like your post. It has been a fine source of information for me in my study. Thank you so much.

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:36 PM by debt management company

Thanks for this tutorial mate. Well, this is my first visit to your site! But I admire the precious time and effort you put into it, especially into interesting articles you share here!

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:39 PM by how to make money online

I enjoyed reading your easy transformations. I see you offer priceless info. Stumbled into this page by chance but I’m sure glad I clicked on that link.

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 12:52 PM by how to fight

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 1:09 PM by rocket piano review

Truthfully pleasant and well made article. The ideas are meaningful and undoubtedly emphasized. Thanks for giving out your thoughts on this one.

# re: Location Formats for ASP.NET MVC

Wednesday, June 29, 2011 1:12 PM by internet marketing strategies

I like to take this chance to state that I really like your post. It has been a fine source of information for me in my study. Thank you so much.

# re: Location Formats for ASP.NET MVC

Wednesday, July 13, 2011 11:43 PM by karlriggs31

I am very glad to see such information which I was searching for a long time.This made very glad..This site has given us an useful information..

# re: Location Formats for ASP.NET MVC

Wednesday, July 20, 2011 12:45 PM by MartinV

Very good and useful. I appreciate the way you explain the things.

# re: Location Formats for ASP.NET MVC

Sunday, July 24, 2011 2:02 AM by deesydia

Thank you for sharing such valuable programming information. I shall keep this bookmarked so that my brother can use it on his other (future) projects.

Dee

# re: Location Formats for ASP.NET MVC

Monday, July 25, 2011 7:42 AM by danjones

I totally agree with this, Thanks

# re: Location Formats for ASP.NET MVC

Monday, July 25, 2011 10:00 AM by bahadur40

Hey there person, only agreed to be evaluating your online world seeking a number of information along with came across your blog. We are in awe of the knowledge which you have on this internet site. The idea demonstrates precisely how you see why issue. Book-marked this article, restarted after. You happen to be wonderful.

# re: Location Formats for ASP.NET MVC

Tuesday, August 09, 2011 10:48 AM by jack121

Whilst this could get a little confusing, the information you have shared is very valuable.

Jack

# re: Location Formats for ASP.NET MVC

Tuesday, August 09, 2011 10:49 AM by jack121

Thanks for sharing this awesome info.

# re: Location Formats for ASP.NET MVC

Thursday, August 11, 2011 11:12 AM by jack121

I agree with Jack. Very confusing but good none the less.

Dave

# re: Location Formats for ASP.NET MVC

Saturday, August 13, 2011 10:05 PM by sharkl

Thanks for the triple post dude!

# re: Location Formats for ASP.NET MVC

Saturday, August 13, 2011 10:06 PM by sharkl

Hmm is this not working?

# re: Location Formats for ASP.NET MVC

Tuesday, August 23, 2011 11:01 PM by sammarks747

The additional Virtual Path Provider code is very beneficial , I think its going be very helpful to many

# re: Location Formats for ASP.NET MVC

Tuesday, August 23, 2011 11:05 PM by sammarks747

I think that all of the the additional Virtual Path Provider code is very beneficial ,from differnt stanpoints and I think its going be very helpful to many users.

great update

SM @ <a href="http://goldfishcarev.com">goldfish care</a>

# re: Location Formats for ASP.NET MVC

Wednesday, August 31, 2011 7:29 AM by maxflik

<a href="www.ukdissertations.com/.../" title="Dissertation Help">Dissertation Help</a>

# re: Location Formats for ASP.NET MVC

Wednesday, August 31, 2011 7:30 AM by maxflik

<a href="www.ukdissertations.com/.../" title="Dissertation Help">Dissertation Help</a>

# re: Location Formats for ASP.NET MVC

Tuesday, September 20, 2011 3:07 AM by jaysontucker82

I also wanted to learn this script apps but I really don't how to begin with.

# re: Location Formats for ASP.NET MVC

Tuesday, November 29, 2011 9:33 AM by bestowadeel

This post is really nice. I always love to make addition to my knowledge and this one really fulfils my need.

# re: Location Formats for ASP.NET MVC

Tuesday, November 29, 2011 9:34 AM by bestowadeel

This post is really nice. I always love to make addition to my knowledge and this one really fulfils my need.

# re: Location Formats for ASP.NET MVC

Tuesday, November 29, 2011 9:35 AM by bestowadeel

This post is really nice. I always love to make addition to my knowledge and this one really fulfils my need.http://socialtadalafil.com/

# re: Location Formats for ASP.NET MVC

Tuesday, November 29, 2011 9:36 AM by bestowadeel

This post is really nice. I always love to make addition to my knowledge and this one really fulfils my need.

http://socialtadalafil.com/

# re: Location Formats for ASP.NET MVC

Friday, December 02, 2011 10:50 PM by video search

Glad to join the discussion. I find this blog indeed very interesting and at the same time informative and educational. Really a nice read.

# re: Location Formats for ASP.NET MVC

Friday, December 02, 2011 10:53 PM by video search

This is the type of blog that I have been looking for. Something that is informative and educational at the same time.

# re: Location Formats for ASP.NET MVC

Thursday, December 15, 2011 3:18 AM by dresseinsorgo

Haha that's rediculous. No way

# re: Location Formats for ASP.NET MVC

Saturday, December 31, 2011 4:59 AM by prathamesh

Really great work,I would like to join your blog anyway.

http://www.olacabs.com

# re: Location Formats for ASP.NET MVC

Friday, February 10, 2012 1:26 PM by dip127527

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my article visitors will find that very useful resources.

Check out these awesome Escorts I found on <a href="http://torontoescortss.com">Toronto Escorts</a>

# re: Location Formats for ASP.NET MVC

Friday, February 10, 2012 1:27 PM by dip127527

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my article visitors will find that very useful resources.

Check out these awesome Escorts I found on <a href="http://torontoescortss.com">Toronto Escorts</a>

# re: Location Formats for ASP.NET MVC

Thursday, April 26, 2012 6:09 AM by HaroldSmith

Exceptional site post. Let me bookmark and check out a lot more often. I enjoy your website.

# re: Location Formats for ASP.NET MVC

Monday, May 07, 2012 12:25 AM by Samantha Knowls

I totally agree what it has said.[url=www.agiftsforgirlfriend.com]gifts for girlfriend[/url]