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"); //不能跳转,不报错
public ActionResult login()
{
return view()
}
filterContext.Result = new RedirectResult(FormsAuthentication.LoginUrl + "?ReturnUrl=" + filterContext.HttpContext.Request.UrlReferrer);
[AttributeUsage(AttributeTargets.Class
[解决办法]
AttributeTargets.Method
[解决办法]
AttributeTargets.Property, AllowMultiple = false)]
public class LoginOnVerificationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext) {
base.OnActionExecuting(filterContext);
if (string.IsNullOrEmpty(HttpContext.User.Identity.Name)) {
string target = HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString());
filterContext.Result = new RedirectResult(string.Format("http://www.yourdomain.com/memberlogin.aspx?target={0}", target));
}
}
}
[LoginOnVerification]
public class GalleryController : Controller {
// your codes.
}
[LoginOnVerification]
public ActionResult Download() {
// your codes.
}