开发者

Can I use an IoC container to create ASP.NET webforms pages and controls?

With ASP.NET MVC it is pretty easy to integrate an IoC container to create the controllers.

Is the same thing also possible with webforms to create the pages and controls and pass them any dependencies? If yes, where do I have to 开发者_开发问答plug it in?


Castle Windsor was the first major IoC container for ASP.NET. See this question discussing how it can be used with Web forms.


Yes you can, here's an example of how Spring.NET does it.


Not directly, as Web Forms does not allow any code to take control of Page and Control instantiation. So something like a Page Factory is not feasible, and you can't do constructor injection.

But there are work arounds/hacks that can buy you setter injection as others have mentioned.


Another good approach would be using the Model-View-Presenter pattern and have your dependencies injected into the presenter's constructor.


I know this question is very old but in the .NET Framework 4.7.2+ you can have constructor injection in ASP.NET WebForm pages and user controls.

From this official blog post:

Areas that Dependency Injection can be used

There are many areas you can use Dependency Injection in WebForms applications now. Here is a complete list.

  • Pages and controls WebForms page User control Custom control
  • IHttpHandler and IHttpHandlerFactory
  • IHttpModule Providers
  • BuildProvider
  • ResourceProviderFactory
  • Health monitoring provider
  • Any ProviderBase based provider created by System.Web.Configuration.ProvidersHelper.InstantiateProvider. e.g. custom session state provider
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜