繁体   English   中英

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

[英]How to add some text in all pages of an application

采访中有人问这个问题。 我有一个具有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 classic和aspx,因此HTTP模块无法对.asp页进行欺骗,可能只有使用临时ISAPI过滤器才能处理每个请求,但通常没有用于将标头写入所有网页

您可以尝试Ctrl + H

并恢复Ctrl + Z

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM