我有点困惑,如果某些会话变量为空,我想导致局部视图而不是redirectResult。可能吗?如果是这样怎么办?我可以看到有PartialViewResult,但是我不知道如何使用它。

 public class XXActionFilter : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext context = HttpContext.Current;

            if ((context.Session[item] == null))
            {
                filterContext.Result = my partial view
                return;
            }


            base.OnActionExecuting(filterContext);
        }
    }

最佳答案

您应该将PartialViewResult的实例传递给Result属性:

public class XXActionFilter : ActionFilterAttribute, IActionFilter
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext context = HttpContext.Current;

        if ((context.Session[item] == null))
        {
            var result = new PartialViewResult
            {
                 ViewName = "PathToView"
            };

            filterContext.Result = result;
            return;
        }


        base.OnActionExecuting(filterContext);
    }
}

关于c# - 如何在OnActionExecuting中返回部分 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28399646/

10-10 12:26