开发者

Redirecting to mobile page in Asp.Net MVC 3

开发者 https://www.devze.com 2023-03-28 22:05 出处:网络
I\'m trying to use a different login page for an Asp.Net MVC application that is modified to fit mobile devices, primarily iPhone/Android. All I basically need is to modify the login view, because the

I'm trying to use a different login page for an Asp.Net MVC application that is modified to fit mobile devices, primarily iPhone/Android. All I basically need is to modify the login view, because the actual content is in a particular part of the application, I'm not trying to make a mobile version of the entire site.

So I tried to follow this: http://www.asp.net/learn/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application

But I don't know the authentication well enough to know exactly how to do the logon action methods for the mobile version. I feel like I'm probably missing a specific mobile post action,and I don't understand what to do with the url passed in the redirect. Here's what I've got so far:

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
            retu开发者_如何学Pythonrn View(model);
        }

And this doesn't work. I get to the mobile login page (or actually so far I've just tried out that the action method works by commenting out the if clause), but when I try to login I just get to the same page again, but strangely without the fields...

What do I need to do to get this to work?


Do you keep the account controller in the Mobile Area? Better practice will be using the mobile Area to keep all your mobile site stuff and redirect the authorization to the 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();
    }


I don't see anywhere you are actually checking to see if it is a mobile device to re-direct:

if (Request.Browser.IsMobileDevice){ }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号