Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1068095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:14:34+00:00 2026-05-16T20:14:34+00:00

What I need to do is capture the result of an expression within an

  • 0

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", BindingFlags.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>";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:14:35+00:00Added an answer on May 16, 2026 at 8:14 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to capture all the rendered output/html of a view so
We have a need to capture signatures from an iOS app – the use
I need to identify (potentially nested) capture groups within regular expressions and create a
I need a regular expression to capture field names listed in a string. They
I need capture specific UIView , but the result is in low quality how
$('input').change(function(){ alert($(this).val()); }); $('input').val(1); This dont work. I need capture the change input with
I need to capture right mouse click event inside a iframe in asp.net using
I need to capture keystokes from a wedge device and prevent any controls or
I need to capture either \d+\.\d+ or \d+ but nothing else. For instance ,
i need to capture the loading time of each http object such as img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.