Changing the default View Location in ASP.NET MVC
[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) && 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.
[…] Changing the default View Location in ASP.NET MVC « The Curious <HEAD/> (tags: asp.net-mvc) […]
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
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
Steve,
You will find it under the Helpers folder
http://nerddinner.codeplex.com/sourcecontrol/changeset/view/23425?projectName=nerddinner#587145
Cheers,
Sumit Thomas
Thank you 2legged : )
Very instructive and useful : D
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).
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 🙂
watch out for the cache, Hanselman’s solution will not work if a desktop browser hits the site first…
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.
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.
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
Good suggestion Claude 🙂 Will bear that in mind next time
Action requires knldgewoe, and now I can act!
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!
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.