Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've already perform this in a web project for asp.net mvc + jquery for "common" browsers and asp.net mvc + jqueryMobile for mobile browsers. I didn't need to create a new route in my global asax. Also these are a pretty sites for Mobile redirection:</p> <p><a href="http://weblogs.asp.net/shijuvarghese/archive/2011/02/25/mobile-enabled-web-apps-with-asp-net-mvc-3-and-jquery-mobile.aspx" rel="nofollow">Mobile enabled web apps with ASP.NET MVC 3 and jQuery Mobile</a> (If you are using mvc3 is much easier) <a href="http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx" rel="nofollow">Walkthrough: Creating an ASP.NET MVC Areas Application Using Multiple Projects</a> <a href="http://dotnetslackers.com/articles/aspnet/a-first-look-at-asp-net-mvc-2.aspx#multiproject-areas" rel="nofollow">A First Look at ASP.NET MVC 2</a></p> <p>So, In my solution I used a Mobile Redirection Attribute, that I tag in my controller's actions that I wanna check for redirection. I saw that in your solution you "ask" the user to click the correct link, does depends on users and is not good (perhaps I didn't catch your intention). However, in this solution you check the for the user agent in the request, to know about the browser that perform the request to your site:</p> <pre><code>/// &lt;summary&gt; /// Attribute for Mobile Redirection when the request action comes from a mobile device. /// &lt;/summary&gt; public class MobileRedirectAttribute : AuthorizeAttribute { private const string defaultMobileController = "Mobile"; #region Properties /// &lt;summary&gt; /// Gets or sets the action. /// &lt;/summary&gt; /// &lt;value&gt;The action.&lt;/value&gt; private string Action { get; set; } /// &lt;summary&gt; /// Gets or sets the controller. /// &lt;/summary&gt; /// &lt;value&gt;The controller.&lt;/value&gt; private string Controller { get; set; } private UrlHelper _urlHelper; /// &lt;summary&gt; /// Sets the URL helper. /// &lt;/summary&gt; /// &lt;value&gt;The URL helper.&lt;/value&gt; internal UrlHelper UrlHelper { set { this._urlHelper = value; } } /// &lt;summary&gt; /// Gets or sets the last URL redirected. /// &lt;/summary&gt; /// &lt;value&gt;The last URL redirected.&lt;/value&gt; internal string RedirectedTo { get; private set; } /// &lt;summary&gt; /// Gets or sets a value indicating whether this instance is mobile device. /// &lt;/summary&gt; /// &lt;value&gt; /// &lt;c&gt;true&lt;/c&gt; if this instance is mobile device; otherwise, &lt;c&gt;false&lt;/c&gt;. /// &lt;/value&gt; internal bool IsMobileDevice { get; private set; } #endregion #region Methods /// &lt;summary&gt; /// Determines whether the specified controller is mobile. /// &lt;/summary&gt; /// &lt;param name="controller"&gt;The controller.&lt;/param&gt; /// &lt;returns&gt; /// &lt;c&gt;true&lt;/c&gt; if the specified controller is mobile; otherwise, &lt;c&gt;false&lt;/c&gt;. /// &lt;/returns&gt; private bool IsMobile(Controller controller) { bool isMobile = controller.Request.Browser.IsMobileDevice; string userAgent = controller.Request.UserAgent.ToLower(); return (isMobile || userAgent.Contains("iphone") || userAgent.Contains("ipod") || userAgent.Contains("blackberry") || userAgent.Contains("mobile") || userAgent.Contains("opera mini") || userAgent.Contains("palm")); } /// &lt;summary&gt; /// Called when [authorization]. /// &lt;/summary&gt; /// &lt;param name="filterContext"&gt;The filter context.&lt;/param&gt; public override void OnAuthorization(AuthorizationContext filterContext) { Controller controller = filterContext.Controller as Controller; this.IsMobileDevice = IsMobile(controller); //test porpouse if (this.IsMobileDevice) { this.RedirectedTo = GetUrlRedirectAction(filterContext.RequestContext); filterContext.Result = new RedirectResult(this.RedirectedTo); } } /// &lt;summary&gt; /// Gets the URL redirect action. /// &lt;/summary&gt; /// &lt;param name="requestContext"&gt;The request context.&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private string GetUrlRedirectAction(RequestContext requestContext) { UrlHelper urlHelper = _urlHelper; if (urlHelper == null) //mocking porpouse; urlHelper = new UrlHelper(requestContext); return urlHelper.Action(this.Action, this.Controller, requestContext.RouteData.Values); } #endregion #region Constructor /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="MobileRedirectAttribute"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="action"&gt;The action.&lt;/param&gt; public MobileRedirectAttribute(string action) { this.Action = action; } /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="MobileRedirectAttribute"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="controller"&gt;The controller.&lt;/param&gt; /// &lt;param name="action"&gt;The action.&lt;/param&gt; public MobileRedirectAttribute(string controller, string action) { this.Action = action; this.Controller = controller; } #endregion } </code></pre> <p>So after this, the only thing you need to do is to tag your controller's actions that you wanna redirect to a different view. Controller logic can be refactor and extract it to a common method.</p> <pre><code>// GET: /MyController/ [MobileRedirectAttribute("MyController","IndexMobile")] public ActionResult Index(string guid) </code></pre> <p>Hope to help, ask if you have any question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload