Hey Microsoft, please extend ASP.NET MVC 4 View Redirection!

There is a lot of discussion going on in the web development world around how to best support mobile devices.

Scott Hanselman’s ( @shanselman ) post ‘A Better ASP.NET MVC Mobile Device Capabilities ViewEngine’ is an interesting look on how to do view redirection using a combination of a custom view engine with extension methods in ASP.NET MVC.

Kevin Montrose’s ( @kevinmontrose ) post ‘Mobile Views in ASP.NET MVC3’ describes how the StackExchange platform uses a view redirection technique to render mobile-friendly views.

And finally, Phil Haack ( @haacked ) posted a sneak peak into the future of ASP.NET MVC with ‘ASP.NET MVC 4 Roadmap’ which describes the tooling around a view redirection technique very similar to what Kevin Montrose describes.

There are all great, but my thought is why not continue down this path of view redirection and make the functionality more generic. This would allow me (the developer) to use the same technique to do things like deliver specific localized views for my Spanish speaking friends.

Conceptually, here’s what I mean, if I want to redirect the mobile views to SomeView.Mobile.cshtml then I can add some code that looks like this:

ViewEngines.Engines.Add(new RazorViewEngine(c => c.IsMobileDevice(), "Mobile"));

Where IsMobileDevice is some extension method that I wrote that does mobile detection. Alternatively, by doing something like this it allows me to similarly do this:

ViewEngines.Engines.Add(new RazorViewEngine(c => c.IsSpanish(), "es"));

Again, IsSpanish is an extension method that I wrote that allows me to do language detection and the views would redirect to SomeView.es.cshtml.

The whole idea here is that while the framework might provide a basic implementation of mobile views, it is open enough to allow the developer to customize it to whatever scenario they can dream up.

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