在Asp.net 4.0 中动态注册HttpModule

using System;
using System.Web;
using Microsoft.Web.Infrastructure;

namespace MvcApplication1
{
    public class CustomModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication ap = sender as HttpApplication;
            if (ap != null)
            {
                ap.Response.Write("汤姆大叔测试PreApplicationStartMethod通过!<br/>");
            }
        }

        public void Dispose()
        {
            //nothing to do here
        }
    }

    public class PreApplicationStartCode
    {
        private static bool hasLoaded;

        public static void PreStart()
        {
            if (!hasLoaded)
            {
                hasLoaded = true;
                //注意这里的动态注册,此静态方法在Microsoft.Web.Infrastructure.DynamicModuleHelper
                Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(CustomModule));
            }
        }
    }
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MvcApplication1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MvcApplication1")]
[assembly: AssemblyCopyright("Copyright ©  2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("98a0cfd0-fae7-43dd-8626-b49f8df939be")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the ‘*‘ as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: System.Web.PreApplicationStartMethod(typeof(MvcApplication1.PreApplicationStartCode), "PreStart")]
时间: 2024-07-31 14:10:11

在Asp.net 4.0 中动态注册HttpModule的相关文章

你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)

一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什么能够动态注册? 汤姆大叔给了如下开篇 通过前面的章节,我们知道HttpApplication在初始化的时候会初始化所有配置文件里注册的HttpModules,那么有一个疑问,能否初始化之前动态加载HttpModule,而不是只从Web.config里读取? 答案是肯定的, ASP.NET MVC3

来篇文章:ASP。NET程序中动态修改web.config中的设置项目 (后台CS代码)

朋友们可以自行测试,我这里都没有问题了,鳖了一上午的问题总算解决了 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; usi

asp.net 2.0中傻瓜式使用soap header

在websevrice 中,soap header是十分重要的哦,主要是安全性的考虑,在asp.net 2.0中,可以简单地应用soap header来进行傻瓜式的应用,更复杂的应用当然要更深入地去看了, 首先,我们写个简单的helloworld的webservice using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespa

Asp.Net MVC3.0中防止跨站的POST

在Form中添加 @Html.AntiForgeryToken(); 在后台的Action中增加 [ValidateAntiForgeryToken] 这个方法还可以添加自定义的参数 @Html.AntiForgeryToken("SaltValue"); 后台的Action中,必需指名Token的值才允许正常提交. [ValidateAntiForgeryToken Salt=("SaltValue")] Asp.Net MVC3.0中防止跨站的POST,布布扣,

在asp.net 2.0中使用SqlBulkCopy类迁移数据

在asp.net 2.0中使用SqlBulkCopy类迁移数据 (转) http://jackyrong.cnblogs.com/archive/2005/08/29/225521.html 我们经常要在一个表中将数据迁移到另一个表,当然,用的方法十分多了.在.net 2.0中,提供了一个sqlbulkcopy类,也可以实现如下的操作,下面简单介绍下.比如一个表如下CREATE TABLE Person3( PersonID int IDENTITY(1,1) PRIMARY KEY, Name

MVC之前的那点事儿系列(6):动态注册HttpModule(转载)

MVC之前的那点事儿系列(6):动态注册HttpModule 文章内容 通过前面的章节,我们知道HttpApplication在初始化的时候会初始化所有配置文件里注册的HttpModules,那么有一个疑问,能否初始化之前动态加载HttpModule,而不是只从Web.config里读取? 答案是肯定的, ASP.NET MVC3发布的时候提供了一个Microsoft.Web.Infrastructure.dll文件,这个文件就是提供了动态注册HttpModule的功能,那么它是如何以注册的呢?

使用asp.net 2.0中的SqlBulkCopy类批量复制数据

介绍:在软件开发中,把数据从一个地方复制到另一个地方是一个普遍的应用. 在很多不同的场合都会执行这个操作,包括旧系统到新系统的移植,从不同的数据库备份数据和收集数据. ASP.NET 2.0有一个SqlBulkCopy类,它可以帮助你从不同的数据源复制数据到SQL SERVER数据库. 本文中我将示范SqlBulkCopy类的不同应用. 数据库设计: 这个数据库的设计还是蛮简单的,它基于Northwind数据库的Products表.另外我还在Northwind数据库中创建了3个表. 详情可以看一

MVC源码解析 - 配置注册 / 动态注册 HttpModule

本来这一篇, 是要继续 Pipeline 的, 但是在 Pipeline之前, 我看到了InitModules()方法, 所以决定, 在中间穿插一篇进来. 这一篇来讲一下 IHttpModule 的加载时机, 以及怎么动态注册 HttpModules. 一. 经典模式下的 InitModules 方法 首先来看一下 InitModules() 方法, 在这个方法中, 初始化了所有的module, 其中包括了配置文件中的和想要动态注册的. 接下来, 看一下方法: private void Init

asp.net Web.config中assembly注册程序集的目的

asp.net的Web.config中用assembly注册程序集 asp.net的Web.config中用assembly注册程序集的目的是什么? .net framewok2.0 提供了表示Framework的各个部件的大量程序集.这些程序集存储在全局程序集缓存中,该缓存是程序集的版本化存储库,可供计算机上的所有应用程序使用(而不像Bin和App_Code目录仅限于特定的应用程序).Framework中的多个程序集都可自动提供给Asp.net应用程序.通过在应用程序的Web.config文件