如何让Autofac在使用Server.Transfer在iframe中加载Web表单页面时注入属性?



编辑:当页面被 Request.Redirect(( 检索时,它确实有效,当使用 Server.Transfer(( 导航 iframe 时出现问题。最好我不想只是将应用程序中对Server.Transfer((的每个引用都更改为Response.Redirect((,因为我不知道它们是否真正可以互换

我正在使用一个带有许多全局变量、上帝类等的遗留 Web 表单应用程序,我一直在尝试改进的第一件事是将一些方法分解到特定的类/接口中,并添加依赖注入。这个应用程序主要通过加载主页面.aspx页面来工作,该页面包含一个加载后续页面的 iframe。我发现当页面在 iframe 中加载时不会发生注入,即使它在外部工作正常。

每个页面,包括Main.aspx,都来自"ProjectBasePage",尽管这似乎不是问题所在。

主.aspx:

<iframe id="webservername" src="" scrolling="no" frameborder="0" style="border:0px;height:13px;width:50px;vertical-align:bottom;"></iframe>

概述.aspx.cs

[InjectProperties]
public partial class Overview : ProjectBasePage
{
public ITableRepository TableRepo { get; set; }

全球

static IContainerProvider _containerProvider;
public IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
protected void Application_Start(object sender, EventArgs e)
{
var builder = new ContainerBuilder();
builder.RegisterType<TableRepository>().As<ITableRepository>();
_containerProvider = new ContainerProvider(builder.Build());
}

真的就是这样,一点也不复杂。如果我手动"注入"Page_Load或通过自己设置属性来工作正常,我只是不知道 Autofac 在哪里跳闸,因为 iframe 加载,我没有足够的经验。我很想将 iframe 转换为母版页或其他东西,但这比我目前能承担的工作要多一些。

> Autofac 已意识到此问题,但不会修复。

https://github.com/autofac/Autofac/issues/337

解决方法是简单地使用手动注射过程: https://autofaccn.readthedocs.io/en/latest/integration/webforms.html#manual-property-injection

最新更新