在OnActionExecuting里 能直接跳出action吗

.Net技术 码拜 8年前 (2016-03-08) 1137次浏览
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Redirect(“/Account”);
filterContext.HttpContext.Response.End();
base.OnActionExecuting(filterContext);
return;

这样写它还会执行ACTION里面的后台代码,有什么办法不让它执行action里面的代码,而直接跳到本人要转到的页面.
解决方案

20

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var url=filterContext.HttpContext.Request.Url.ToString();
            if (url.EndsWith("/"))
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectResult(url + "/", true);
            }
        }

这个是之前给别人回答的强制给url加个/后缀的,设定filterContext.Result ,而不是用Response.Redirect


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在OnActionExecuting里 能直接跳出action吗
喜欢 (0)
[1034331897@qq.com]
分享 (0)