Change controller based on device

I am working with an MVC4 web application that has 3 anchor points - desktop, phone and tablet.

I have configured the display modes so that the view depends on the device accessing the site. I also use media queries and jQuery mobile to help with mobile site development.

When the site is accessed from the phone, less of the stored database data is displayed to the end user. For this reason, intensive processing on the controller is not required for phones.

Is there an elegant way to change the controller or action method used when the consumer app is a phone? I would like the url to stay the same and also not have validation inside the controller action method for the device.

Any way to do this?

+3


source to share


1 answer


Sample post here:

http://weblogs.asp.net/shijuvarghese/archive/2011/02/25/mobile-enabled-web-apps-with-asp-net-mvc-3-and-jquery-mobile.aspx

when redirecting the user to the mobile area of ​​your MVC project using a filter attribute.



Best I can find, I know you said you didn't want to change urls .. so maybe you could write a filter attribute to apply to your controller .. so the action is ignored?

Something like this is possible (not tested), but its nasty as it has to be constantly maintained and is actually just injecting more code into your worker pipeline which might feel a bit "magic", personally I would go with separate scopes ,

public class MobileDeviceAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.Browser.IsMobileDevice)
        {
            if (filterContext.ActionDescriptor.ActionName == "foo")
                filterContext.HttpContext.Response.RedirectToRoute("bar");
        }
    }
}

      

+1


source







All Articles