Redirect action to mobile view
public ActionResult Home()
{
return View();
}
This is what I have for my current site wit开发者_如何学JAVAhin the HomeController. How can I detect if the action is called from a mobile device, and if it is, re-direct to MobileHome.aspx instead of Home.aspx.
I don't need to know the details of the device, because I am implementing the view in JQuery Mobile, which should adjust itself correctly depending on the view it's rendered in.
You may find the following blog post useful.
The following is an override on the Controller class. I have not tested this, so consider it pseudo code:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (this.Request.Browser.IsMobileDevice && filterContext.Result is ViewResultBase)
{
var viewResult = filterContext.Result as ViewResultBase;
viewResult.ViewName = "Mobile" + viewResult.ViewName;
}
base.OnActionExecuted(filterContext);
}
You can use the Request.Browser.IsMobileDevice to determine if the device is mobile (obviously), and then check to see if the result it a view. However, changing the view name is not sufficient if you pass an actual view to the result of your action.
精彩评论