The Curious <HEAD/>

Changing the default View Location in ASP.NET MVC

Posted in ASP.NET by 2leggedspider on June 10, 2009

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) &&
								request.Browser.IsMobileDevice)
				{
					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: ,

3 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
    RRaveen
    Founder CodeGain.com

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

    Hi,
    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..

    Rgds

    Steve Mew


Leave a Reply