Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 346003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:08:12+00:00 2026-05-12T11:08:12+00:00

I’m working my way through some ASP.NET MVC reading and I have a web

  • 0

I’m working my way through some ASP.NET MVC reading and I have a web app at work that I’ll be migrating from WebForms to MVC. One of the feature requests I expect to get in the process is to have a simplified view returned if the user is coming from a mobile device.

I can’t quite see where the best place is to implement that type of logic. I’m sure there’s a better way than adding an if/else for Browser.IsMobileDevice in every action that returns a view. What kind of options would I have to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:08:12+00:00Added an answer on May 12, 2026 at 11:08 am

    Update: This solution has a subtle bug. The MVC framework will call into FindView/FindPartialView twice: once with useCache=true, and if that doesn’t return a result, once with useCache=false. Since there’s only one cache for all types of views, mobile users may end up seeing desktop views if a desktop browser was first to arrive.

    For those interested in using custom view engines to solve this problem, Scott Hanselman has updated his solution here:

    http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx

    (Apologies for the answer hijack, I just don’t want anyone else to have to go through this!)

    Edited by roufamatic (2010-11-17)


    The first thing you want to do is introduce the Mobile Device Browser File to your project. Using this file you can target what ever device you want to support without having to know the specifics of what those devices send in their headers. This file has already done the work for you. You then use the Request.Browser property to tailor which view you want to return.

    Next, come up with a strategy on how you want to organize your views under the Views folder. I prefer to leave the desktop version at the root and then have a Mobile folder. For instance the Home view folder would look like this:

    • Home
      • Mobile
        • iPhone
          • Index.aspx
        • BlackBerry
          • Index.aspx
      • Index.aspx

    I have to disagree with @Mehrdad about using a custom view engine. The view engine serves more than one purpose and one of those purposes is finding views for the controller. You do this by overriding the FindView method. In this method, you can do your checks on where to find the view. After you know which device is using your site, you can use the strategy you came up with for organizing your views to return the view for that device.

    public class CustomViewEngine : WebFormViewEngine
    {
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            // Logic for finding views in your project using your strategy for organizing your views under the Views folder.
            ViewEngineResult result = null;
            var request = controllerContext.HttpContext.Request;
    
            // iPhone Detection
            if (request.UserAgent.IndexOf("iPhone",
       StringComparison.OrdinalIgnoreCase) > 0)
            {
                result = base.FindView(controllerContext, "Mobile/iPhone/" + viewName, masterName, useCache);
            }
    
            // Blackberry Detection
            if (request.UserAgent.IndexOf("BlackBerry",
       StringComparison.OrdinalIgnoreCase) > 0)
            {
                result = base.FindView(controllerContext, "Mobile/BlackBerry/" + viewName, masterName, useCache);
            }
    
            // Default Mobile
            if (request.Browser.IsMobileDevice)
            {
                result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
            }
    
            // Desktop
            if (result == null || result.View == null)
            {
                result = base.FindView(controllerContext, viewName, masterName, useCache);
            }
    
            return result;
        }
    }
    

    The above code allows you set the view based on your strategy. The fall back is the desktop view, if no view was found for the device or if there isn’t a default mobile view.

    If you decide to put the logic in your controller’s instead of creating a view engine. The best approach would be to create a custom ActionFilterAttribute that you can decorate your controller’s with. Then override the OnActionExecuted method to determine which device is viewing your site. You can check this blog post out on how to. The post also has some nice links to some Mix videos on this very subject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.