【问题标题】:The previous page link appears in address bar in MVC3 Razor application?上一页链接出现在 MVC3 Razor 应用程序的地址栏中?
【发布时间】:2013-01-14 12:28:20
【问题描述】:

我正在使用 MVC3/Razor。登录网站后,出现首页,但不明白为什么会出现上一页的url。

场景:当我将用户从登录页面重定向到主页时,地址栏中的 url 显示为“http://localhost:55104/Account/LogOn”而不是“http://localhost:55104/Home/Index” "

帐户控制器

 // GET: /Account/LogOn
    public ActionResult LogOn()
    {
        if (HttpContext.User.Identity.IsAuthenticated == true)
        {
            return RedirectToAction("Index", "Home");
        }
        else
        {
            return View();
        }
    }

    // POST: /Account/LogOn
    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            try
            {
                if (AppAuthentication.Authenticate(model.UserName, model.Password, "10.0.3.18"))
                {
                    string userName = model.UserName;
                    var user = new List<String>();
                    MySqlConnection con = DAL.GetMySqlConnection();
                    MySqlCommand cmd = new MySqlCommand("SELECT user_id, user_fname FROM users  WHERE  user_code='" + userName + "' AND user_treeCode <> 'xxx'", con);
                    MySqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        user.Add(rdr.GetString(0));
                        user.Add(rdr.GetString(1));
                    }
                    con.Close();
                    FormsAuthentication.SetAuthCookie(user[0], model.RememberMe);
                    Session["userID"] = user[0];
                    Session["userName"] = user[1];
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ViewBag.OpenID = "Invalid credentials ";
                }
            }
            catch (Exception ex)
            {
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

Gloabal.asax

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );           
    }

 protected void Application_Start()
    {                       
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

【问题讨论】:

  • 这可能与路由有关 - 你能发布你的路由配置吗?
  • 感谢您的帮助。帖子已更新。

标签: asp.net-mvc asp.net-mvc-3 c#-4.0 routing asp.net-mvc-routing


【解决方案1】:

您的登录表单必须是 Ajax 表单。在这种情况下,RedirectToAction 就像一个 View 方法。

【讨论】:

    【解决方案2】:

    你可以这样用

     if (ModelState.IsValid)
        {
            if (string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = Url.Action("Index", "App");
            }
            if (Request.IsAjaxRequest())
            {
                return Json(new { returnUrl = returnUrl });
            }
            return Redirect(returnUrl);
        }
    

    在视图中:

    <% using (Ajax.BeginForm(
        "LogOn", 
        null, 
        new AjaxOptions { 
            HttpMethod = "POST", 
            OnSuccess = "success" 
        }, 
        new { 
            id = "SignInForm", ReturnUrl = Request.QueryString["ReturnUrl"] 
        })) { %>
            <<Page HTML Controls>>
            <input type="submit" value="Log On" />
    <% } %>
    
    <script type="text/javascript">
    function success(context) {
        var returnUrl = context.get_data().returnUrl;
        if (returnUrl) {
            window.location.href = returnUrl;
        } else {
            // TODO: update the target form element with the returned partial html
        }
    }
    </script>
    

    【讨论】:

    • 感谢您的帮助。我尝试使用您的解决方案。
    • 我尝试使用您的建议,但我已经在地址栏http://localhost:1413/Account/LogOn 中显示了一条消息“未定义”。然后,如果我按 F5,我将被重定向到 Home/Index
    猜你喜欢
    • 2012-04-02
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多