开发者

Capturing Inline Expression Result

What I need to do is capture the result of an expression within an MVC view (sample below).

I have also provided a stub Processor demonstrating what I want to achieve. Basically I want to divert the Target of the Action into some arbitrary string buffer that I can manipulate later. However the Target property is readonly. Can this be overridden using reflection as I am doing below (setting the target to an arbitrary writer with a StringBuffer at its core). This doesn;t work correctly and I keep getting a Null Reference exception when I execute inline().

Can anyone help? Code below (GetInlineResult being the key method).

View

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% MvcApplication5.Processor.Absorb(() => {%>     
        reverse = (string) ->
          string.split('').reverse().join ''

        alert reverse '.eeffoC yrT'
    <%}); %>
    <%=MvcApplication5.Processor.Render()%>
</asp:Content>

Processor Code

public class Processor
{
    public static void Absorb(Action inline)
    {
        string input = GetInlineResult(inline);
        string output = Process(input);
        File.WriteAllText("SOME_PATH", output);  
    }

    private static string Process(string input)
    {
        string output = input;
        /* IRRELEVANT PROCESSING */
        return output;
    }

    private static string GetInlineResult(Action inline)
    {
        // create writers etc.
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter htmltw = new HtmlTextWriter(sw);

        // set internal value to new writer
        FieldInfo fi = inline.GetType().GetField("_target", Bi开发者_运维百科ndingFlags.Instance | BindingFlags.NonPublic);
        fi.SetValue(inline, htmltw);

        // execute 
        inline();

        // get contents
        return sb.ToString();
    }

    public static string Render()
    {
        /* GENERATE LINK TO RENDERED FILE <script type="tpl" src="SOME_PATH"> */
        return "<script type='tpl' src='SOME_URL'></script>";
    }
}


Problem solved.

I was overthinking this problem and replacing the generated view class with a text writer rather than replacing the views TextWriter instance.

Updated GetInlineResult (currently in VERY crude form)

    private static string GetInlineResult(Action inline)
    {
        // create writers etc.
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter htmltw = new HtmlTextWriter(sw);


        object view = inline.Target;
        FieldInfo field = view.GetType().GetField("__w");
        HtmlTextWriter tw = field.GetValue(view) as HtmlTextWriter;

        field.SetValue(view, htmltw);
        // execute 
        inline();

        field.SetValue(view, tw);

        // get contents
        return sb.ToString();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜