微软企业库5.0学习笔记(10)ASP.NET模块依赖注入
您可以使用HTTP模块,一个到ASP.NET HttpApplicationState类的扩展,在Global.asax编写代码强制ASP.NET在每一个页面请求时自动注入依赖的对象,就像在ASP.NET Web窗体应用程序中讨论的一样.
下列方法显示了一个合适的方法能够获取PreRequestHandlerExecute事件将它自己注入到ASP.NET的执行流水线,在每个页面请求中通过容器的BuildUp方法运行Http模块,并获取OnPageInitComplete事件。当OnPageInitComplete执行时模块代码按照所有的控件树运行,并通过容器的BuildUp方法处理每个控件。
BuildUp方法获取已经存在的对象实例,处理并填充类的依赖,返回实例。如果没有依赖则返回最初的实例。
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.Unity;
namespace Unity.Web
{
public class UnityHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}
public void Dispose() { }
private void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
IHttpHandler currentHandler = HttpContext.Current.Handler;
HttpContext.Current.Application.GetContainer().BuildUp(
currentHandler.GetType(), currentHandler);
// User Controls are ready to be built up after page initialization is complete
var currentPage = HttpContext.Current.Handler as Page;
if (currentPage != null)
{
currentPage.InitComplete += OnPageInitComplete;
}
}
// Build up each control in the page's control tree
private void OnPageInitComplete(object sender, EventArgs e)
{
var currentPage = (Page)sender;
IUnityContainer container = HttpContext.Current.Application.GetContainer();
foreach (Control c in GetControlTree(currentPage))
{
container.BuildUp(c.GetType(), c);
}
context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute;
}
// Ge
相关文档:
关于需要用到的JS文件请到地址:http://d.download.csdn.net/down/2387457/taomanman免费下载;
下面介绍其使用方法:
第一步:
到我提供的地址下载JS文件,并解压到项目某个目录下,你自己定啦。
第二步:
在需要用到时间控件的aspx页面中添加该JS文件中的WdatePicker.js文件。
如<script src="../JS/My97DatePick ......
这两天看《道不远人-----深入解析ASP.NET2.0控件开发》这本书,看完第二章内容后,想总结下“设置自定义ASP.NET服务器控件TagPrefix的几种方法”,以便以后查阅,以下面code编写的控件为例,由于重点不是控件编写,所以写了个非常简单的控件,姑且叫它EmailInput
Code
1using System;
2using ......
/// <summary>
/// 根据指定参数返回BitMap对象
/// 引用如下:
/// using System.Drawing;
/// 调用例子如下:
......
+++ 修改WebConfig文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="connStr" value="Data Source=ora11g;uid=scott;pwd=tiger;unicode=true"/>
</appSettings>
<connectionStrings>
<ad ......