Insert ASP Tags and UserControls into CMS Content

Go To StackoverFlow.com

0

Here's the "main question" I guess:

Is there a ".NET way" to get at a request very early on (either via HttpHandler or HttpModule) and inject standard asp.net markup, either in the form of standard controls, or user/custom controls, and have the framework treat it like a page that it is compiling dynamically?

More Background

Ok, so there's this custom CMS we use that does a great job of building fairly complicated sites. It has a nice templating system, user permissions, easy to install, etc. However, one feature we want to add is being able to add dynamic content.

After the CMS goes out and does "its thing" the result is essentially a giant HTML string that gets sent back in the response. The rendering engine is contained in a class that handles this "cms stuff". Right now this happens in a standard page lifecycle, but there's no reason it can't be called independently.

I've been able to get close with both a httpHandler and httpModule, but the problem is that what I really need is to take the "cms html string", add in my own .net controls, and then have the framework take that new string and build a control tree from it (along with a viewstate, etc).

The closest I got was with the HttpModule which during PreRequestHandlerExecute attached a filter to the Response. This filter just looked for places to replace {{replaceme}} with a label tag markup (as a proof-of-concept...example below). I thought this would work since from what I had read this was before the control tree was created, but all that happened was the asp.net markup was written to the page (not rendered by .NET).

Another thing I've tried is to take complete control via an HttpHandler. This almost worked as well (also an example below)...the problem here is that while I can build my own control tree, and even execute an ascx control, there is no place to merge all of these things together (remember I already have an entire html document string, I'm trying to merge it at runtime and pass it to .NET).

I feel like I'm very close but missing some very easy way to pass most of this work off to .NET...

Httpmodule

    private void PreRequestHandlerExecute(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;

        context.Response.Filter = new PageFilter(context.Response.Filter);
    }

Filter (From a SO example I think)

    public override void Write(byte[] buffer, int offset, int count)
    {
        string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);

        // ---------------------------------
        // Wait for the closing </html> tag
        // ---------------------------------
        Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);

        if (!eof.IsMatch(strBuffer))
        {
            responseHtml.Append(strBuffer);
        }
        else
        {
            responseHtml.Append(strBuffer);
            string finalHtml = responseHtml.ToString();

            finalHtml = finalHtml.Replace("{{replaceme}}", @"<asp:Label id=""lblMyTest"" runat=""server"" Text=""This is a label that was replaced by my responsefilter"" />");

            // Transform the response and write it back out

            byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);

            responseStream.Write(data, 0, data.Length);
        }
    }

The Test HttpHandler

    public void ProcessRequest(HttpContext context)
    {

        MyBasePage page = new MyBasePage();
        Test tpage = (Test)BuildManager.CreateInstanceFromVirtualPath("~/Test.ascx", typeof(Test));

        var htmlform = new System.Web.UI.HtmlControls.HtmlForm();
        var panel = new Panel();

        panel.Controls.Add(new Label() { Text = "Hello World" });
        panel.Controls.Add(tpage);

        htmlform.Controls.Add(panel);
        page.Controls.Add(htmlform);
        page.ProcessRequest(context);
    }
2012-04-03 21:18
by FirstDivision


1

You may have a lot of trouble trying to do this. If I understand you, you have a chunk of HTML that comes out of the CMS, and you want to turn that back into a control tree, which you can merge with more controls (from ASP.Net markup) and then generate the final HTML.

But that's the opposite of what ASP.Net does. It parses markup with ASP.Net controls to generate a control tree, and then generates HTML from that control tree. Nothing in ASP.Net parses arbitrary HTML. I think you'd need to completely re-write the .aspx handler, duplicating all its parsing and tree building functions.

I would try to find another way to implement this using your CMS. You're subverting the system, and that could only lead to a very hard to maintain mess.

2012-04-03 21:31
by Carl Raymond
Almost. I want to take the chunk of html, insert some asp.net markup, and then pass that whole thing back to .NET to handle the rendering of the asp.net controls.

It's almost as if I want to make believe that this string I have in memory is really an .aspx page (with no code-behind) and tell .NET "please treat this like a dynamically-compiled page" - FirstDivision 2012-04-03 21:53

I guess this will be as close as I can get. This is the SharePoint 2010 Page Processing model which I guess I was hoping to emulate, albeit in a much simpler manner: http://msdn.microsoft.com/en-us/library/ms498550.asp - FirstDivision 2012-04-09 16:29
Ads