如何设置布局,在行动过滤器空? [英] how can i set layout to null in action filter?

查看:102
本文介绍了如何设置布局,在行动过滤器空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是更多钞票来设置过滤器动作布局空?例如:

Is it posible to set the layout to null in action filter? For example

public ActionResult SomeAction()
    {
        return PartialView();
    }

我要呈现的一些动作

I want to render some action with

@Html.Action("someaction")

它的工作现在。

it works for now.

但我想在2模式下才能使用此动作:如儿童和大师一样对不同的情况。我可以在考虑这个布局设置为空

But i want to use this action in 2 modes : like child and like master for different situations. I can set Layout to null in view for this

查看:

@{
   if(condtition)
   {
        Layout = null;
   }
}

但我想找到更优雅的方式:)

But i want to find more elegant way :)

我爱:

无局部动作。

public ActionResult SomeAction()
        {
            return View();
        }

和过滤器设置布局为空,如果动作是孩子

and in filter set the layout to null if action is child

 public class LayoutFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
           if(filterContext.IsChildAction)
           {
               //set the layout to NULL here!

           }
        }
    }

有更多钞票?任何想法?

It is posible? Any ideas?

推荐答案

在您的例子已经重写了 OnActionExecuting 事件,但发生这种情况为时尚早。这些行动甚至还没有执行,也没有返回一个ActionResult,你已经尝试设置它的布局。

In your example you have overriden the OnActionExecuting event but this happens way too early. The actions hasn't even executed yet nor returned an ActionResult and you are already attempting to set its Layout.

等待它完成,通过重写 OnActionExecuted 事件,从结果属性 filterContext ,如果它是一个的ViewResult 及其 MasterName 属性设置为空值:

Wait for it to complete, by overriding the OnActionExecuted event, retrieve the Result property from the filterContext and if it is a ViewResult set its MasterName property to null:

public class LayoutFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var result = filterContext.Result as ViewResult;
        if (result != null)
        {
            result.MasterName = null;
        }
    }
}

这篇关于如何设置布局,在行动过滤器空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆