如何使用reprository和service调用webapi控制器参数化构造函数

编程入门 行业动态 更新时间:2024-10-08 22:19:34
本文介绍了如何使用reprository和service调用webapi控制器参数化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用reprository和web-API的参数化构造函数的示例示例 我尝试过: 我试过这个:

public class TestUsersUserControlController:ApiController { private readonly IConnectUserRepository ConnectUserRepository; public TestUsersUserControlController() {} public TestUsersUserControlController(IConnectUserRepository ConnectUserRepository) { this .ConnectUserRepository = ConnectUserRepository; ....... .. 此参数化构造函数 未点击并始终 null }

解决方案

为此,您需要使用依赖注入(DI)。如果你谷歌web api依赖注入你会发现很多例子,如果你知道你想要提前使用的DI框架(Castle Windsor,Autofac,Ninject,Unity等),它可能会有所帮助,但是她是MS文档使用Unity ASP.NET Web API 2中的依赖注入| Microsoft Docs [ ^ ]

I need some sample example with parameterized constructor using reprository and web-API What I have tried: I tried this:

public class TestUsersUserControlController : ApiController { private readonly IConnectUserRepository ConnectUserRepository; public TestUsersUserControlController() { } public TestUsersUserControlController(IConnectUserRepository ConnectUserRepository) { this.ConnectUserRepository = ConnectUserRepository;.........this parameterized constructor is not hitting and getting always null }

解决方案

For that to work you need to use Dependency Injection (DI). If you google "web api dependency injection" you'll find lots of examples, it might help if you know the DI framework you want to use in advance (Castle Windsor, Autofac, Ninject, Unity etc), however her is the MS docs which uses Unity Dependency Injection in ASP.NET Web API 2 | Microsoft Docs[^]

更多推荐

如何使用reprository和service调用webapi控制器参数化构造函数

本文发布于:2023-11-17 11:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   控制器   函数   参数   webapi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!