Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping/Modifying Html Result

Tags:

c#

asp.net-mvc

Basically we are in a major hacky situation. We have a few web pages that is linked to from an other site. However, the requirement is that this site has the same layout as the site that linked to us. This was originally done by requesting the original page, scraping the layout, and wrapping out content in their layout.

This was rather simple in Web Forms as we could simply create a subclassed Page, that overrides the Render method and then wrapped anything we produced in the external sites layout. However, now this project is being rewritten in ASP.NET MVC.

How can we get acccess to the HTML result created by the MVC actions, modify them according to our needs and output the modified result to the browser?

like image 511
Kasper Holdum Avatar asked Jan 18 '26 09:01

Kasper Holdum


1 Answers

You can use ActionFilterAttribute.OnResultExecuted Method

You can see more sample on ActionFilters here:

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

EDIT

There is a great blog post on this topic:

http://weblogs.asp.net/imranbaloch/archive/2010/09/26/moving-asp-net-mvc-client-side-scripts-to-bottom.aspx

To sum it up, you need to tweak your output. As far as I understand you need to use RegEx to get the portion you need to tweak out of the full HTML and you can do that like below:

This is a Helper class:

public class HelperClass : Stream {

    //Other Members are not included for brevity

    private System.IO.Stream Base;

    public HelperClass(System.IO.Stream ResponseStream)
    {
        if (ResponseStream == null)
            throw new ArgumentNullException("ResponseStream");
        this.Base = ResponseStream;
    }

    StringBuilder s = new StringBuilder();

    public override void Write(byte[] buffer, int offset, int count) {

        string HTML = Encoding.UTF8.GetString(buffer, offset, count);

        //In here you need to get the portion out of the full HTML
        //You can do that with RegEx as it is explain on the blog pots link I have given
        HTML += "<div style=\"color:red;\">Comes from OnResultExecuted method</div>";

        buffer = System.Text.Encoding.UTF8.GetBytes(HTML);
        this.Base.Write(buffer, 0, buffer.Length);
    }

}

This is you filter:

public class MyCustomAttribute : ActionFilterAttribute {

    public override void OnActionExecuted(ActionExecutedContext filterContext) {

        var response = filterContext.HttpContext.Response;

        if (response.ContentType == "text/html") {
            response.Filter = new HelperClass(response.Filter);
        }
    }
}

You need to register this on Global.asax file Application_Start method like below:

protected void Application_Start() {

    //there are probably other codes here but I cut them out to stick with point here

    GlobalFilters.Filters.Add(new MyCustomAttribute());
}
like image 158
tugberk Avatar answered Jan 20 '26 23:01

tugberk