简体   繁体   English

如何自定义WebViewPage执行输出?

[英]How to customize WebViewPage executation output?

I am going to inject some elements in every view of our Asp.Net MVC application. 我将在Asp.Net MVC应用程序的每个视图中注入一些元素。 so to do that (I found this: Custom WebViewPage inject code when razor template is rendering answer) I have subclassed the WebViewPage and I have overridden the ExecutePageHierarchy like below: 这样做(我发现了这一点: 在剃刀模板呈现答案时,自定义WebViewPage注入代码 )我将WebViewPage子类化,并且覆盖了ExecutePageHierarchy如下所示:

   public override void ExecutePageHierarchy()
    {
        base.ExecutePageHierarchy();
        string output = Output.ToString();
        if (MyHelper.InitializationRequired)
            output = MyHelper.GetPageHeader() + output + MyHelper.GetPageFooter();
        //--------------
        Response.Clear();
        Response.Write(output);
        Response.End();
    }

by this code we can wrap all of the output markup with some header and some footer elements such as scripts or additional tags which i want to do that. 通过此代码,我们可以用一些页眉和页脚元素(例如脚本或我想要这样做的其他标签)包装所有输出标记。

BUT in this way we lost the Layout completely! 但是这样我们就完全失去了Layout because of clearing the response. 因为清除了响应。

my main question is that, how to inject some HTML markups exactly before or exactly after WebViewPage 's output by preserving the content of response which maybe there are some other views or the layout? 我的主要问题是, 如何通过保留响应的内容(可能还有其他视图或布局) WebViewPage的输出之前或之后注入一些HTML标记

您应该使用MVC的布局系统...具有主布局模式的完整功能:=)

Finally i found a trick to do it by this way: 最终,我找到了一种通过这种方式完成此操作的技巧:

 public override void ExecutePageHierarchy()
    {
        var tmp = OutputStack.Pop();
        var myWriter = new StringWriter();
        OutputStack.Push(myWriter);
        base.ExecutePageHierarchy();

        tmp.Write(
            string.Format("<div> Header of [{0}]</div> {1} <div> Footer of [{0}]</div>",
            VirtualPath,
            myWriter.ToString()));
    }

it works well generally and it wraps the output of view by header and footer. 它通常工作良好,并且按页眉和页脚包装视图的输出。 but in my scenario i should able to access to some flags which will assigned on executing view. 但是在我的场景中,我应该能够访问将在执行视图时分配的一些标志。 so i must check them after view execution: 所以我必须在查看执行后检查它们:

 public override void ExecutePageHierarchy()
    {
        var tmp = OutputStack.Pop();
        var myWriter = new StringWriter();
        OutputStack.Push(myWriter);
        base.ExecutePageHierarchy();
        if (MyHelper.InitializationRequired)
           tmp.Write(
            string.Format("<div> Header of [{0}]</div> {1} <div> Footer of [{0}]</div>",
            VirtualPath,
            myWriter.ToString()));
        else
           tmp.Write(myWriter.ToString());
    }

this approach works well for me. 这种方法对我来说很好。 so i posted it, maybe help some one;) 所以我发布了它,也许可以帮助一个;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM