views:

52

answers:

2

Is it possible, from within ASP.NET MVC, to route to different controllers or actions based on the accessing device/browser?

I'm thinking of setting up alternative actions and views for some parts of my website in case it is accessed from the iPhone, to optimize display and functionality of it. I don't want to create a completely separate project for the iPhone though as the majority of the site is fine on any device.

Any idea on how to do this?

A: 

Best bet would be a custom action filter.

All you have to do is inherit from ActionMethodSelectorAttribute, and override the IsValidRequest class.

public class [IphoneRequest] : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
             // return true/false if device is iphone

Then in your controller

[IphoneRequest]
public ActionResult Index()
RPM1984
Oh and i think to work out if the device is iphone:string agent = controllerContext.HttpContext.Request.UserAgent;return agent.Contains("iPhone")
RPM1984
+1  A: 

Mix: Mobile Web Sites with ASP.NET MVC and the Mobile Browser Definition File

Don't know if the above helps as I havn't watched it yet.

And this one;

How Would I Change ASP.NET MVC Views Based on Device Type?

griegs