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

时间:2014-07-19 10:01:13

标签: c# asp.net

有人在接受采访时会问这个问题。 我有一个应用程序,有50000个Asp.net Web表单,10000个html页面和20000个asp页面。 我想在body标签后的所有页面中添加一些东西我怎么能实现这个

2 个答案:

答案 0 :(得分:2)

你可以通过Custom 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页面进行操作,可能只有ad hoc ISAPI filter可以处理每个请求,但通常没有用于将标题写入所有网页

答案 1 :(得分:0)

您可以尝试 Ctrl + H

并恢复 Ctrl + Z