if (Request.HttpMethod == "GET") { if (ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice) { @Html.ActionLink("Desktop view", "SwitchView", "ViewSwitcher", new { mobile = false, returnUrl = Request.Url.PathAndQuery }, new { rel = "external" }) } else { @Html.ActionLink("Mobile view", "SwitchView", "ViewSwitcher", new { mobile = true, returnUrl = Request.Url.PathAndQuery }, new { rel = "external" }) } }
public class ViewSwitcherController : Controller { public RedirectResult SwitchView(bool mobile, string returnUrl) { if (Request.Browser.IsMobileDevice == mobile) HttpContext.ClearOverriddenBrowser(); else HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop); return Redirect(returnUrl); } }
首先在ActionLink的地方以Request.Url.PathAndQuery取得呼叫前的網址,
然後在 Action 的地方處理完後就可以再轉回原來的網址了。