我正在将 autofac 与 asp.net 一起使用。在 Global.asax 我注册了我所有的网页:
AssertNotBuilt();
// Register Web Pages
m_builder.RegisterAssemblyTypes(typeof(AboutPage).Assembly)
.Where(t => t.GetInterfaces().Contains(typeof(IHttpHandler)))
.AsSelf().InstancePerLifetimeScope();
m_container = m_builder.Build();
m_wasBuilt = true;
然后我使用自定义的 httpHandler 来获取当前网页:
public class ContextInitializerHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//Get the name of the page requested
string aspxPage = context.Request.Url.AbsolutePath;
if (aspxPage.Contains(".aspx"))
{
// Get compiled type by path
Type webPageBaseType = BuildManager.GetCompiledType(aspxPage).BaseType;
// Resolve the current page
Page page = (Page)scope.Resolve(webPageBaseType);
//process request
page.ProcessRequest(context);
}
}
public bool IsReusable
{
get { return true; }
}
}
一切正常,但是当它进入 web page_load 时,我看到页面上存在的所有 asp 控件都是空的。为什么它们为空,我该如何初始化它们?