我正在尝试对 Asp.Net MVC 应用程序使用不同的登录页面,该应用程序经过修改以适合移动设备(主要是 iPhone/Android)。我基本上需要的只是修改登录 View ,因为实际内容位于应用程序的特定部分,我不想制作整个网站的移动版本。
所以我尝试遵循这个:http://www.asp.net/learn/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application
但我不太了解身份验证,无法确切知道如何执行移动版本的登录操作方法。我觉得我可能错过了特定的移动后操作,并且我不明白如何处理重定向中传递的网址。这是我到目前为止所得到的:
public ActionResult LogOn()
{
string returnUrl = Request.QueryString["ReturnUrl"];
if ((returnUrl != null) && returnUrl.StartsWith("/Mobile/",
StringComparison.OrdinalIgnoreCase))
{
return RedirectToAction("LogOnMobile", "Account",
new { ReturnUrl = returnUrl });
}
return View();
}
public ActionResult LogOnMobile(string returnurl)
{
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
这行不通。我进入移动登录页面(或者实际上到目前为止,我刚刚尝试通过注释掉 if 子句来尝试操作方法),但是当我尝试登录时,我只是再次进入同一页面,但奇怪的是没有字段...
我需要做什么才能让它发挥作用?
最佳答案
您是否将帐户 Controller 保留在移动区域中?更好的做法是使用移动区域来保存所有移动站点内容并将授权重定向到 AccountController。
public ActionResult Login()
{
string returnUrl = Request.QueryString["ReturnUrl"];
if ((returnUrl != null) && returnUrl.StartsWith("/Mobile/", StringComparison.OrdinalIgnoreCase))
return RedirectToAction("Login", "Account", new { Area = "Mobile", ReturnUrl = returnUrl });
return ContextDependentView();
}
关于asp.net - 在 Asp.Net MVC 3 中重定向到移动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7113430/