asp.net-mvc - 上一页链接出现在 MVC3 Razor 应用程序的地址栏中吗?

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

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

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

账户 Controller

 // 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);
    }

全局.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);
    }

最佳答案

您的登录表单必须是 Ajax 表单。在这种情况下,RedirectToAction 的作用类似于 View 方法。

关于asp.net-mvc - 上一页链接出现在 MVC3 Razor 应用程序的地址栏中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318403/

相关文章:

javascript - jQuery 验证——不验证

c# - MVC 长时间运行 EF 存储过程调用

asp.net - JSON 在 IE7 中未定义

c# - Telerik MVC 网格自定义命令更新网格

c# - 获取嵌套在列表中的最大计数列表

multithreading - 在线程上创建工作队列

c# - 通过ninject "ToProvider"方法解决依赖,该方法基于HttpContext.Current

c# - 当有复合主键并且只想使用一个时使用 db.Find

c# - MVC3 模型验证不适用于双

.net - 我们是否有 bool 值的 Button down 属性