Chandra
Chandra

Reputation: 92

How to add some text in all pages of an application

This question is asked by someone in an interview. I have one application which have 50000 Asp.net web forms, 10000 html pages and 20000 asp pages. I want to add something in all pages after body tag how can i achive this

Upvotes: 1

Views: 224

Answers (2)

Bengi Besceli
Bengi Besceli

Reputation: 3748

You can try Ctrl+H

and to restore Ctrl+Z

Upvotes: 0

invernomuto
invernomuto

Reputation: 10211

You could do the trick by a Custom HttpModule

Module c#

using System;
using System.Web;
public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest += 
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += 
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source, 
         EventArgs e)
    {
    // Create HttpApplication and HttpContext objects to access
    // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = 
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx") || fileExtension.Equals(".html") || fileExtension.Equals(".asp"))
        {
            context.Response.Write("<h1><font color=red>" +
                "HelloWorldModule: Beginning of Request" +
                "</font></h1><hr>");
        }
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = 
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx")|| fileExtension.Equals(".html")|| fileExtension.Equals(".asp"))
        {
            context.Response.Write("<hr><h1><font color=red>" +
                "HelloWorldModule: End of Request</font></h1>");
        }
    }

    public void Dispose() { }
}

Web.config

<configuration>
  <system.webServer>
    <modules>
      <add name="HelloWorldModule" type="HelloWorldModule"/>
    </modules>
  </system.webServer>
</configuration>

EDIT

As commented by @Andreas there are two distint pipeline to precess asp classic and aspx, so the HTTP module can't do the trick with .asp pages, probably only with an ad hoc ISAPI filter can work every request, but generally there is not used for writing an header to all web pages

Upvotes: 2

Related Questions