- public class LogHelperService
- {
- public void Add()
- {
-
- }
- public string Read()
- {
- return "日志读取";
- }
- }
- public class HomeController : BaseController
- {
-
- //通过一个字段来存储
- LogHelperService logHelperService;
-
- public HomeController(LogHelperService loghelperservice)
- {
- this.logHelperService = loghelperservice;
- logHelperService.Add();
- }
- }
- public interface ILogHelperService
- {
- void Add();
- string Read();
- }
services.AddTransient
- public class HomeController : BaseController
- {
-
- //通过一个字段来存储
- ILogHelperService ilogHelperService;
-
- public HomeController(ILogHelperService iloghelperservice)
- {
- this.ilogHelperService = iloghelperservice;
- ilogHelperService.Add();
- }
- }
- //[FromServices] 这个标注 告诉MVC 这个参数不用用户传递过来的get post方式。
- //而是依赖容器传递过来的
- //还可以接收传递过来的值
- public IActionResult Contact([FromServices] ILogHelperService loghelper)
- {
- return Content(loghelper.Read());
-
- }