Changing the default View Location in ASP.NET MVC

Posted in ASPNETMVC, C# by Sumit Thomas on June 10, 2009

[tweetmeme style=”compact”]After 8 hours of training in ASP.NET MVC by a guy from Microsoft I starting revisiting the ways in which I’ve implemented some of the functionality in my existing project done using MVC. The training was just a walkthrough of what I already know about ASP.NET MVC from the internet. One of the questions I put forth to the trainer, which he termed as interesting was, how to change the default view location in MVC. Apart from his I’ll get back to you on this answer, one of my colleagues in the room was vociferous in declaring that it is not possible at all as none of the MVC tutorials talk about it ๐Ÿ˜

I googled and binged for answers and found few…

I found this post Organize your views in ASP.Net MVC very useful in scenarios where I have more than one Controller which needs to share the same View location.

I wanted to check if there are any other ways of doing the same and so I twittered Scott Hanselman, the guy himself to find if he can give me any pointers and he replied…

shanselmanR @2leggedspider Derive from WebFormsViewEngine, override just FindView(). Look at the NerdDinner code on Codeplex at the MobileViewEngine.

He was talking about this piece of code in NerdDinner.

public class MobileCapableWebFormViewEngine : WebFormViewEngine
		public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
			ViewEngineResult result = null;
			var request = controllerContext.HttpContext.Request;

			//This could be replaced with a switch statement as other advanced / device specific views are created
			if (UserAgentIs(controllerContext, "iPhone"))	{
				result = base.FindView(controllerContext, "Mobile/iPhone/" + viewName, masterName, useCache);

			// Avoid unnecessary checks if this device isn't suspected to be a mobile device
			if (request.Browser.IsMobileDevice)
				//TODO: We are not doing any thing WinMobile SPECIAL yet!

				//if (UserAgentIs(controllerContext, "MSIEMobile 6"))	{
				//  result = base.FindView(controllerContext, "Mobile/MobileIE6/" + viewName, masterName, useCache);
				//else if (UserAgentIs(controllerContext, "PocketIE") && request.Browser.MajorVersion >= 4)
				//  result = base.FindView(controllerContext, "Mobile/PocketIE/" + viewName, masterName, useCache);

				//Fall back to default mobile view if no other mobile view has already been set
				if ((result == null || result.View == null) &&
					result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);

			//Fall back to desktop view if no other view has been selected
			if (result == null || result.View == null)
				result = base.FindView(controllerContext, viewName, masterName, useCache);

			return result;

		public bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest)
			return (controllerContext.HttpContext.Request.UserAgent.IndexOf(userAgentToTest,
							StringComparison.OrdinalIgnoreCase) > 0);

Though the above code helps in detecting if the user is accessing the site from a mobile device and redirect the request to a particular view location, you can customize it for your needs.

Btw, if you have not checked NerdDinner yet, I suggest you should. It is one of the best ways to learn MVC.

Another approach I found useful is from Phil Haack Grouping Controllers with ASP.NET MVC.

Let me know if you came across any other approach or best practice relevant to this.

Tagged with: , ,

15 Responses

Subscribe to comments with RSS.

  1. links for 2009-06-20 « MyNotePad said, on June 21, 2009 at 4:05 am

    […] Changing the default View Location in ASP.NET MVC ยซ The Curious <HEAD/> (tags: asp.net-mvc) […]

  2. RRaveen said, on July 2, 2009 at 1:54 pm

    Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    Founder CodeGain.com

  3. Steve Mew said, on October 31, 2009 at 5:59 am

    Was look into NerdDinner and there is no public class MobileCapableWebFormViewEngine : WebFormViewEngine.

    It doesn’t exist.

    Can you possibly clarify where exactly this code can be found ?
    Any pointers greatly appreciated..


    Steve Mew

  4. SD!!! said, on February 13, 2010 at 2:53 am

    Thank you 2legged : )

    Very instructive and useful : D

  5. Titus Mantia said, on December 31, 2010 at 5:33 am

    Hi – really great site you have created. I had a blast reading this posting. I did want to publish a comment to inform you that the layout of this web site is very aesthetically sweet. I used to be a graphic designer, now I am a simple employee for a software development company. I have always had fun working with computers and am attempting to learn computer code in my spare time (which there is never enough of lol).

  6. cityville said, on January 5, 2011 at 2:35 pm

    i was starting to assume i may well end up being the only person which thought about this, at least now i realize im not weird ๐Ÿ™‚ i’ll make it a point to look into a few different blogposts when i get a bit of caffeine in me, cheers ๐Ÿ™‚

  7. LOVE_MOSS_NOT said, on April 5, 2011 at 7:54 am

    watch out for the cache, Hanselman’s solution will not work if a desktop browser hits the site first…

  8. Adrian Frisinger said, on April 15, 2011 at 11:43 am

    Thank you for sharing excellent informations. Your site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and just couldn’t come across. What an ideal web-site.

  9. Ambrose said, on September 13, 2011 at 12:46 pm

    Fantastic publish. I was looking at this web site and I’m impressed! Very useful details particularly the final portion ๐Ÿ™‚ I care for this kind of details a lot. I wanted this specific information for some time. Thank you and good luck.

  10. Claude said, on September 13, 2011 at 9:54 pm

    Have you ever regarded including extra videos to the content to help keep the readers more entertained? I mean I just read over the article of yours and it was very excellent but because I’m really a visual learner,I discovered that to get more helpful. Just my my idea, Good luck

    • Sumit Thomas said, on September 23, 2011 at 3:49 pm

      Good suggestion Claude ๐Ÿ™‚ Will bear that in mind next time

    • Lyndee said, on January 21, 2015 at 11:47 pm

      Action requires knldgewoe, and now I can act!

  11. Millie said, on April 29, 2013 at 1:14 pm

    Outstanding post however , I was wondering if you could write a
    litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Many thanks!

  12. Adam leedam said, on October 12, 2017 at 12:09 am

    Itโ€™s great to come across a blog every once in a while that isnโ€™t the same out of date rehashed material. Fantastic read.
    Iโ€™ve bookmarked your site, and Iโ€™m adding your RSS feeds to my Google account.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: