38-在Action中注入依赖对象

在Action中注入,我们需要使用FromServices标注

public IActionResult TestReject([FromServices] ILogHelperService logHelper)
{
    //其他地方 可以调用
    return Content(logHelper.Read());
}Code language: PHP (php)

当然 也需要在StartUP中注册

FromServices这个标注 就是告诉MVC 这个参数 不是用户传递过来的 get post方式。而是依赖容器传过来的。

还可以接收传过来的值

public IActionResult TestReject([FromServices] ILogHelperService logHelper,int? id)Code language: CSS (css)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注