Under the Hood of Display Mode in MVC 4
This post is part of ASP.NET MVC 4 Article Series.
I am little late in publishing this post as I was busy with my 1st codeplex project. Very soon I will post about my 1st codeplex project. Till that, enjoy this MVC 4 series and do let me know your valuable feedback.
In our last post, we have seen how we can use Display Mode feature of MVC 4 to enable browser specific view. In this post we will examine how in built Display Mode works in MVC 4.
We know that we can create view for mobile browser by adding .mobile suffix in view name. For e.g. index.mobile.cshtml. And this mobile view will be automatically rendered in mobile browser and we also know how to add new Display Mode in MVC 4 application but the question is that we have not added any Display Mode for .mobile suffix than also how it is rendered automatically in mobile browser. We will try to debug it in this post.
Get started for the Learn with Fun
- Create a new MVC 4 application.
- Open Global.asax.cs and put break point on Application_Start as displayed in below image.
- Now press F5 and wait till break point hit.
- Now open Immediate Window and type as shown below.
- As you can see the value of System.Web.WebPages.DisplayModes.Modes.Count is 2 which mean there are two in built Display Mode in MVC 4 application. Let take it further to know more about these two in built Display Mode.
- First of all stop debugging by pressing Shift + F5 and import System.Web.WebPages namespace at the top of Global.asax.cs file so we need not to write full qualified name in debugging process.
- Now again press F5 and wait till break point hit.
- Type following in the Immediate Window.
We have tried to create instance of DefaultDisplayMode class and assigned it with Display Mode at 0th position. Examining its DisplayModeId properties, we found that its value is Mobile and hence this Display Mode is responsible for rendering mobile view in mobile browser. Examining ContextCondition properties, we found that it reference the <.cctor>b__b(System.Web.HttpContextBase) method and hence this method is responsible for validating current request context that whether it is from mobile browser or not (We will also dig into this method at later in this post).
Same way let also inspect Display Mode at 1st position just as we inspected of 0th position. Type following in the Immediate Window.
As we can see in Immediate Window that DisplayModeId is set to blank or empty and ContextCondition is set to null which means if all preceding Display Mode’s ContextCondition fail at that time this Display Mode will be taken in consider. So keep patient while adding Display Mode with null ContextCondition and make sure you add it after adding all other Display Mode otherwise succeeding Display Mode will never taken in consideration.
In earlier in this post, while we examined Display Mode at 0th position at that time we noticed that <.cctor>b__b(System.Web.HttpContextBase) method is responsible for validating current request context. To dig more on this method do as follow.
- Open Visual Studio Command Prompt and type ildasm.
- Open System.Web.WebPages.dll and navigate to <.cctor>b__b(System.Web.HttpContextBase) method under System.Web.WebPages.DisplayModes.
- Now double clicking on method name will open a new window.
Examining code in that window you will find that it is detecting mobile browser with System.Web.HttpBrowserCapabilitiesBase::get_IsMobileDevice() which is equivalent to C# Request.Browser.IsMobileDevice.
So in this post, we tried to examine in built Display Mode of MVC 4. Hope this post would be helpful.