易截截图软件、单文件、免安装、纯绿色、仅160KB

微软企业库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


相关文档:

asp.net 发布网站

asp.net 发布网站时有三个选项:
1、允许更新此预编译站点:asp.net web 页面通常包含两个页面,一个即 .aspx 页,还有一个 .aspx.cs 文件,后一个文件是基本委托的事件响应文件代码;此处允许更新此预编译站点的意思就是:.aspx 页可根据需要进行一定的更新,而 .aspx.cs 编译成的 .dll 保持不变;另外,如果发布网站时只 ......

asp.net 获取当前URL的正确方法

HttpContext.Current.Request.Url.ToString() 并不可靠。
如果当前URL为
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5
通过HttpContext.Current.Request.Url.ToString()获取到的却是
http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼&fra ......

ASP.NET页面传值_第六篇_Cookie

+++ Cookie01.aspx页面
++ 页面代码如下:
<asp:TextBox ID="TextBox1" runat="server" ForeColor="Red" Width="182px">Name</asp:TextBox>
<asp:Button ID="BtnCookie" runat="server" OnClick="BtnCookie_Click" Text="BtnCookie" /><br />
++ 后台代码如下:
protected void BtnCookie_Cl ......

ASP.NET页面传值_第九篇_Cache

+++ PassDatatableByCache01.aspx页面
++ 页台代码如下:
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="用Cache传数据集"></asp:Button>
++ 后台代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
  string connStr = "Data Source=ora11g;uid=sc ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号