{"id":153,"date":"2012-05-03T17:00:39","date_gmt":"2012-05-03T15:00:39","guid":{"rendered":"http:\/\/www.christiaannieuwlaat.nl\/?p=153"},"modified":"2012-05-03T17:00:39","modified_gmt":"2012-05-03T15:00:39","slug":"using-mvc-displaymode-for-domain-overridable-views","status":"publish","type":"post","link":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/","title":{"rendered":"Using MVC 4 DisplayModes to achieve per domain overridable views"},"content":{"rendered":"<p>One of the new features of the Microsoft ASP.NET MVC 4 framework is the concept of display modes. By default the framework uses the display mode functionality to &#8220;dynamically&#8221; select the mobile version of a view ( when present ), when you&#8217;re site is viewed on a mobile device.<\/p>\n<p>In short this means it will use the <code>&lt;viewname&gt;.mobile.cshtml\/.vbhtml<\/code> file as the view when available, and if it&#8217;s not available it will fall back to the normal <code>&lt;viewname&gt;.cshtml\/.vbhtml<\/code> file. (<em>I&#8217;m assuming you are using Razor syntax for your views.<\/em>).<\/p>\n<p>There are tutorials (like <a href=\"http:\/\/channel9.msdn.com\/Events\/TechDays\/Techdays-2012-the-Netherlands\/2302\" title=\"Going mobile with ASP.NET MVC 4 by Erik van Appeldoorn\">in this video on channel 9<\/a>) that show how to extend this functionality to create a view for a specific device, like an iPhone or iPad. But this is just one of the possibilities.<\/p>\n<p>Just the other day at the office, I&#8217;ve had to rewrite a website to support mobile devices. It&#8217;s been written on top of ASP.NET MVC 2, so I thought it was only logical to rewrite it with ASP.NET MVC 4. Seems to me like the obvious path to choose, until I took a good look at the way the current site worked.<\/p>\n<p>The site was written in a way that it could be loaded from multiple domains, and used a different set of views and resources based on the host header. To get that done in MVC2 I&#8217;ve created my own ViewEngine derivative at the time. As I didn&#8217;t want to do that again ( and potentionally lose Razor syntax goodness etc.. ) in this version, it was a challenge to get my head around it, but I&#8217;ve got it working like a charm. The key player in the solution is the MVC 4 DisplayModes functionality.<\/p>\n<p>In short: If you can use the display modes to select a different view based on the user agent, you can do more-or-less the same based on the host header. Now I know there are multiple ways to get this done, but as I had to support an arbitrary configurable amount of &#8220;themes&#8221;, I chose to do it on a more-or-less centralised approach. Summarized I&#8217;ve created 2 <code>DefaultDisplayMode<\/code> derivative classes and overrode the<br \/>\n<code>GetDisplayInfo<\/code> method to incorporate the &#8216;load the theme based file&#8217; logic. I&#8217;ve created 2 classes, because I wanted to separate the mobile and the non-mobile logic, but you could do it in one if you&#8217;d like. All I&#8217;ve had to do after this is to insert them in the DisplayModes collection so they will be used by the MVC framework.<\/p>\n<p>I&#8217;ll be posting a more elaborate version (including some source code) of the solution shortly. But for now, I hope you&#8217;ve got the idea how I&#8217;ve tackled it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the new features of the Microsoft ASP.NET MVC 4 framework is the concept of display modes. By default the framework uses the display mode functionality to &#8220;dynamically&#8221; select the mobile version of a view ( when present ), when you&#8217;re site is viewed on a mobile device. In short this means it will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,21,1],"tags":[25,26,27],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Per domain overridable views using MVC 4 DisplayModes<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Per domain overridable views using MVC 4 DisplayModes\" \/>\n<meta property=\"og:description\" content=\"One of the new features of the Microsoft ASP.NET MVC 4 framework is the concept of display modes. By default the framework uses the display mode functionality to &#8220;dynamically&#8221; select the mobile version of a view ( when present ), when you&#8217;re site is viewed on a mobile device. In short this means it will [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/\" \/>\n<meta property=\"og:site_name\" content=\"Christiaan Nieuwlaat .nl\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/christiaan.nieuwlaat\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-03T15:00:39+00:00\" \/>\n<meta name=\"author\" content=\"chris\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@krizzje\" \/>\n<meta name=\"twitter:site\" content=\"@krizzje\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"chris\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/\",\"name\":\"Per domain overridable views using MVC 4 DisplayModes\",\"isPartOf\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\"},\"datePublished\":\"2012-05-03T15:00:39+00:00\",\"dateModified\":\"2012-05-03T15:00:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.christiaannieuwlaat.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using MVC 4 DisplayModes to achieve per domain overridable views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#website\",\"url\":\"https:\/\/www.christiaannieuwlaat.nl\/\",\"name\":\"Christiaan Nieuwlaat .nl\",\"description\":\"IT Whizzkid behind the wheels of steel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef\",\"name\":\"chris\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g\",\"caption\":\"chris\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Per domain overridable views using MVC 4 DisplayModes","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/","og_locale":"en_US","og_type":"article","og_title":"Per domain overridable views using MVC 4 DisplayModes","og_description":"One of the new features of the Microsoft ASP.NET MVC 4 framework is the concept of display modes. By default the framework uses the display mode functionality to &#8220;dynamically&#8221; select the mobile version of a view ( when present ), when you&#8217;re site is viewed on a mobile device. In short this means it will [&hellip;]","og_url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/","og_site_name":"Christiaan Nieuwlaat .nl","article_publisher":"https:\/\/www.facebook.com\/christiaan.nieuwlaat","article_published_time":"2012-05-03T15:00:39+00:00","author":"chris","twitter_card":"summary_large_image","twitter_creator":"@krizzje","twitter_site":"@krizzje","twitter_misc":{"Written by":"chris","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/","url":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/","name":"Per domain overridable views using MVC 4 DisplayModes","isPartOf":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#website"},"datePublished":"2012-05-03T15:00:39+00:00","dateModified":"2012-05-03T15:00:39+00:00","author":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef"},"breadcrumb":{"@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/2012\/05\/03\/using-mvc-displaymode-for-domain-overridable-views\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.christiaannieuwlaat.nl\/"},{"@type":"ListItem","position":2,"name":"Using MVC 4 DisplayModes to achieve per domain overridable views"}]},{"@type":"WebSite","@id":"https:\/\/www.christiaannieuwlaat.nl\/#website","url":"https:\/\/www.christiaannieuwlaat.nl\/","name":"Christiaan Nieuwlaat .nl","description":"IT Whizzkid behind the wheels of steel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.christiaannieuwlaat.nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/db0e2d25ff2c1c9a5c2f86c22081bbef","name":"chris","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.christiaannieuwlaat.nl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7e1e034197de6790df1ed0095c77a136?s=96&d=mm&r=g","caption":"chris"}}]}},"_links":{"self":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/153"}],"collection":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":0,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.christiaannieuwlaat.nl\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}