1.HttpModel和Global.asax对比
简单说一下HttpModule可以多个 Global.asax只有一个

Global.asax 里可以处理Application_Start事件 HttpModule无

一个虚拟目录只能有一个Global.asax,一般尽量不用,这样方便和别的程序整合
2.Global.asax模块
 void Application_Start(object sender, EventArgs e)  
  {
  // Code that runs on application startup
  }
  void Application_End(object sender, EventArgs e)  
  {
  // Code that runs on application shutdown
  }   
  void Application_Error(object sender, EventArgs e)  
  {  
  // Code that runs when an unhandled error occurs
  }
  void Session_Start(object sender, EventArgs e)  
  {
  // Code that runs when a new session is started
  }
  void Session_End(object sender, EventArgs e)  
  {
  // Code that runs when a session ends.  
  // Note: The Session_End event is raised only when the sessionstate mode
  // is set to InProc in the Web.config file. If session mode is set to StateServer  
  // or SQLServer, the event is not raised.
  }
3.HttpModule实现
创建一个类实现HttpModule接口,在web.config中设置
<httpModules>
  <add type="HttpControler" name="HttpControler"/>
</httpModules>

其中“HttpControler”为创建类名称,里面要实现init和dispose方法

====================================================================

引自msdn:

HTTP 模块与 Global.asax 文件
可以在应用程序的 Global.asax 文件中实现模块的许多功能,这使您可以响应应用程序事件。但是,模块相对于 Global.asax 文件具有如下优点:模块可以进行封装,因此可以在创建一次后在许多不同的应用程序中使用。通过将它们添加到全局程序集缓存 (GAC) 并将它们注册到 Machine.config 文件中,可以跨应用程序重新使用它们。有关更多信息,请参见全局程序集缓存。

但是,使用 Global.asax 文件有一个好处,那就是您可以将代码放在其他已注册的模块事件(如 Session_Start 和 Session_End 方法)中。此外,Global.asax 文件还允许您实例化可在整个应用程序中使用的全局对象。

当您需要创建依赖应用程序事件的代码并且希望在其他应用程序中重用模块时,或者不希望将复杂代码放在 Global.asax 文件中时,应当使用模块。当您需要创建依赖应用程序事件的代码但不需要跨应用程序重用它时,或者需要订阅不可用于模块的事件(如 Session_Start)时,应当将代码放在 Global.asax 文件中。

===================================================================

引自codebetter.com :

Global.asax? Use HttpModules Instead!

Posted by karlseguin on June 12, 2006

In a previous post, I talked about HttpHandlers – an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are probably more common than HttpHandlers, but could still stand to be advertised a bit more.

HttpModules are incredibly easy to explain, so this will hopefully be a short-ish post. Simply put, HttpModules are portable versions of the global.asax. So, in your HttpModule you’ll see things like BeginRequest, OnError, AuthenticateRequest, etc. Actually, since HttpModules implement IHttpModule, you actually only get Init (and Dispose if you have any cleanup to do). The Init method passes in the HttpApplication which lets you hook into all of those events. For example, I have an ErrorModule that I use on most projects:

using System;
using System.Web;
using log4net;

namespace Fuel.Web
{
 public class ErrorModule : IHttpModule
 {
  #region IHttpModule Members
  public void Init(HttpApplication application)
  {
   application.Error += new EventHandler(application_Error);  
  }      
  public void Dispose() { }
  #endregion

public void application_Error(object sender, EventArgs e)
  {
    //handle error
  }
 }
}

Now, the code in my error handler is pretty simple:

HttpContext ctx = HttpContext.Current;
//get the inner most exception
Exception exception;
for (exception = ctx.Server.GetLastError(); exception.InnerException != null; exception = exception.InnerException) { }
if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404)
{
 logger.Warn(“A 404 occurred”, exception);
}
else
{
 logger.Error(“ErrorModule caught an unhandled exception”, exception);
}

I’m just using a log4net logger to log the exception, if it’s a 404 I’m just logging it as a warning.

You can do this just as easily with a global.asax, but those things aren’t reusable across projects. That of course means that you’ll end up duplicating your code and making it hard to manage. With my ErrorModule class, I just put it in a DLL, drop it in my bin folder and add a couple lines to my web.config under <system.web>:

<httpModules>
 <add  name=”ErrorModule” type=”Fuel.Web.ErrorModule, Fuel.Web” />
</httpModules>

And voila, I have a global error in place.

In almost all cases, you should go with HttpModules over global.asax because they are simply more reusable. As another example, my localization stuff uses an HttpModule as the basis for adding a multilingual framework to any application. Simply drop the DLL in the bin and add the relevant line in your web.config and you’re on your way. Here’s the important code from that module:

public void Init(HttpApplication context)
{
 context.BeginRequest += new EventHandler(context_BeginRequest);
}
public void Dispose() {}
private void context_BeginRequest(object sender, EventArgs e)
{
 HttpRequest request = ((HttpApplication) sender).Request;
 HttpContext context = ((HttpApplication)sender).Context;
 string applicationPath = request.ApplicationPath;
 if(applicationPath == “/”)
 {
    applicationPath = string.Empty;
 }
 string requestPath = request.Url.AbsolutePath.Substring(applicationPath.Length);
 //just a function that parses the path for a culture and sets the CurrentCulture and CurrentUICulture
 LoadCulture(ref requestPath);
 context.RewritePath(applicationPath + requestPath);
}

If you are developing a shrink-wrap product, you don’t have a choice but to use HttpModules, because the last thing you want is to ship a global.asax which the user must use, overwriting the code in his own global.asax.

The only time you want to use Global.asax is when using OutputCaching with the VaryByCustom property. As far as I know, the GetVaryByCustomString function _must_ be placed in the global.asax file.

Anyways, switching from Global.asax to HttpModules is pretty straightforward. So I encourage you to look at where it makes sense (ie, where you see the potential for reuse across applications) and make it so.

HttpModule 与 Globle.asax相关推荐

  1. ASP.NET中全局变量

    1.同一页面: ViewState; 2.不同页面:Session; 3.application     如:  1//在Web.Config中  2<configuratin>  3   ...

  2. Global.asax详解

    在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...

  3. ASP.Net中自定义Http处理及应用之HttpModule篇

    HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response).HttpHandler功能的实现通过实现IHttpHandle ...

  4. Asp.Net 构架(HttpModule 介绍)

    Http 请求处理流程 和 Http Handler 介绍 这两篇文章里,我们首先了解了Http请求在服务器端的处理流程,随后我们知道Http请求最终会由实现了IHttpHandler接口的类进行处理 ...

  5. HttpHandler与HttpModule区别

    ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...

  6. 从请求管道深入剖析HttpModule的实现机制,有图有真相

    想要了解底层的原理必须对请求处理过程和页面的生命周期有点了解才方便您入门学习一下内容: 关于请求处理过程和页面的生命周期将会在接下来的日子为大家做一个深入的讲解. HttpModule的实现机制如下: ...

  7. HttpModule与HttpHandler详解(转)

    一 asp.net请求的处理过程 ------------------- HttpModule必须要掌握的东西 HttpHandler 必须要掌握的东西,非常有用 以上两个的实例 ---------- ...

  8. 不使用配置文件动态注册HttpModule

    在asp.net 4.0中,提供了一种不通过修改配置文件注册Module的方法.从.net3.5开始,新提供的PreApplicationStartMethodAttribute特性可以应用在程序集上 ...

  9. MVC全局用户验证之HttpModule

    在请求进入到MVC的处理mcvHandler之前,请求先到达HttpModule,因此可以利用HttpModule做全局的用户验证. HttpModule MVC5之前的版本基于system.web. ...

  10. ASP.NET进阶(8):HttpModule和HttpApplication

    前面三节讲了控件的构造.呈现和数据绑定,我想该差不多了.本想讲一个自定义控件来终结控件部分,但是我个人不太喜欢控件这些东西,所以也就懒的写相关的内容,抱歉了.虽然我不喜欢使用控件,但我还是喜欢整个We ...

最新文章

  1. 使用了SDRAM,使用了分散加载文件,出现HardFault_Handler
  2. 扩展SpringMVC WebMvcConfigurerAdapter ||全面接管SpringMVC @EnableWebMvc
  3. 17 HTTP编程入门
  4. java学习(47):带参无返回
  5. JavaScript中的“ this”关键字
  6. 201.数字范围按位与
  7. EDA技术实用教程 | 复习五 | 端口模式
  8. wp文件转shp_【转载】将E00文件转换成shp文件
  9. python制作一个超强的加密软件
  10. 大数据(3i)Sqoop安装和操作
  11. 京东与淘宝孰优孰劣?
  12. BSGS 大步小步算法
  13. 解决项目部署到阿里云服务器邮件发送失败的方法
  14. ViewPage2简单使用
  15. 运行内存数据加密加密
  16. sql 除法计算一直 为0原因及解决方案
  17. iOS开发——网络请求案例汇总
  18. mybatis-plus无spring框架
  19. 小学数学题升维思考,降维打击
  20. 新概念英语二,Lesson 2

热门文章

  1. 智慧环卫车辆监控管理系统方案
  2. 史上最全VPS+云服务器运维面板汇总(收藏)
  3. Android 分区布局详解
  4. 2021年5月系统集成项目管理工程师案例分析真题讲解(2)
  5. 松本行弘的程序世界。
  6. Excel格式刷使用技巧
  7. 全文来了!任正非:全球经济长期衰退,华为要把活下来作为主要纲领
  8. java进出口食品安全信息管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
  9. 使用HDTunePro检测硬盘快速上手教程
  10. 二 详解VBA编程是什么