I’m using autofac with asp.net. In Global.asax I register all my web pages:
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;
Then I use a custom httpHandler to get the current web page:
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; }
}
}
All works ok, but then when it enters the web page_load, I see that all the asp controls that exist on the page are null. Why are they null and how do I initialize them?
I figured it out. The pages that I registered aren’t compiled like the pages that I can take from the context in my http handler:
and these are the pages that I need that do hold all the controls. The problem is, I can’t register them at my http handler because they are dynamic and look in a form of somewebpage_aspx and the assembly is App_Web_somewebpage.aspx.cdcab7d2.r3x-vs2n, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
So the solution (or hack..) was not to register the web pages, and instead resolve the page controls from the scope: