日期:2014-05-17  浏览次数:20393 次

MVC中的跳转问题。。
写了个Controller基类 用来做登录验证。   重写下方法 但跳转不好用。

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (HttpContext.User.Identity.Name == "")
            {
            Response.Write("4567890-");    //可以输出
            RedirectToAction("login", "home");   //不能跳转,不报错
            Redirect("~/dome.htm");      ////不能跳转,不报错
            Response.Write("<script type=\"text/javascript\">    window.location = \"/dome.htm\"</script>");  //可以跳转
         }
      }

请问RedirectToAction、Redirect为啥不用用呢。。

我这么做十分用户是否登录的判断是否有问题?

谢谢
MVC

------解决方案--------------------
RedirectToAction("login", "home");   //不能跳转,不报错

只是跳转到Action,所以你必选要先新建Controller(homeController),然后再在Controller里新建视图
 public ActionResult login()
        {     
return view()   
}

右键login新建view
------解决方案--------------------
filterContext.Result = new RedirectResult(FormsAuthentication.LoginUrl + "?ReturnUrl=" + filterContext.HttpContext.Request.UrlReferrer);

------解决方案--------------------
试试2楼的的做法。

一般我们做登录检测的话都是用Attribute比较多,这种方式相对来说比较灵活,你可以参考一下。
Attribute定义:
[AttributeUsage(AttributeTargets.Class 
------解决方案--------------------
 AttributeTargets.Method