如何在应用程序的所有页面中添加一些文本

本文关键字:添加 文本 应用程序 | 更新日期: 2023-09-27 18:13:31

这个问题是有人在面试中问的。我有一个应用程序,其中有50000 Asp.net web表单,10000 html页面和20000 asp页面。我想在正文标签之后的所有页面中添加一些东西我怎么才能实现这个

如何在应用程序的所有页面中添加一些文本

你可以通过自定义HttpModule

来实现这个技巧

模块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>

编辑

正如@Andreas所评论的那样,有两个不同的管道来处理asp经典和aspx,所以HTTP模块不能处理。asp页面,可能只有一个特别的ISAPI过滤器可以工作每个请求,但通常不用于为所有网页写头

可以试试Ctrl+H

和恢复Ctrl+Z