带有autofac的asp.net-webform上的所有控件均为空
我正在将autofac与asp.net一起使用。在Global.asax中,我注册了我的所有网页:带有autofac的asp.net-webform上的所有控件均为空,asp.net,null,controls,httphandler,autofac,Asp.net,Null,Controls,Httphandler,Autofac,我正在将autofac与asp.net一起使用。在Global.asax中,我注册了我的所有网页: AssertNotBuilt(); // Register Web Pages m_builder.RegisterAssemblyTypes(typeof(AboutPage).Assembly) .Where(t => t.GetInterfaces().Contains(typeof(IHttpHandler))) .AsSelf().InstancePerLifetimeSc
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; }
}
}
一切正常,但当它进入网页加载时,我看到页面上存在的所有asp控件都为空。为什么它们是空的,我如何初始化它们?我找到了答案。我注册的页面与我可以从http处理程序的上下文中获取的页面不同:
string aspxPage = context.Request.Url.AbsolutePath;
Type webPageBaseType = BuildManager.GetCompiledType(aspxPage);
这些是我需要的页面,可以保存所有的控件。问题是,我无法在我的http处理程序中注册它们,因为它们是动态的,看起来是somewebpage\u aspx的形式,程序集是App\u Web\u somewebpage.aspx.cdcab7d2.r3x-vs2n,版本=0.0.0.0,区域性=中性,PublicKeyToken=null
因此,解决方案(或黑客…)不是注册网页,而是从以下范围解析页面控件:
ILifetimeScope scope = IocInitializer.Instance.InitializeCallLifetimeScope();
Type webPageType = BuildManager.GetCompiledType(aspxPage);
Page page = (Page)Activator.CreateInstance(webPageType);
foreach (var webPageProperty in webPageType.GetProperties(BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public))
{
if (scope.IsRegistered(webPageProperty.PropertyType))
{
var service = scope.Resolve(webPageProperty.PropertyType);
webPageProperty.SetValue(page, service, null);
}
}
你可能需要在这里澄清一些事情。看起来你在做一些非常不标准的事情。Autofac wiki告诉我们如何正确地与web表单集成()。您似乎在使用处理程序来完成一些工作,而不是Autofac提供的模块集成。用户如何访问您的页面?是否每个请求都通过该处理程序传递?如果只是“新建”页面而不是解析页面,会发生什么?还是空的?