ASP.NET 5 MVC(Visual Studio 2015):一个名为Inject的关键字

时间:2022-10-15 03:31:19

I've just installed visual studio 2015 and played around the ASP.NET 5 template (MVC) and I saw a keyword called inject that has been used in the view. Is it for dependency injection, and if yes how does it work? Assuming the below code:

我刚刚安装了visual studio 2015并玩了ASP.NET 5模板(MVC),我看到了一个名为inject的关键字已在视图中使用过。是依赖注入,如果是,它是如何工作的?假设以下代码:

[Authorize]
public class AccountController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly IEmailSender _emailSender;
    private readonly ISmsSender _smsSender;
    private readonly ApplicationDbContext _applicationDbContext;
    private static bool _databaseChecked;

    public AccountController(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager,
        IEmailSender emailSender,
        ISmsSender smsSender,
        ApplicationDbContext applicationDbContext)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _emailSender = emailSender;
        _smsSender = smsSender;
        _applicationDbContext = applicationDbContext;
    }

    //
    // GET: /Account/Login
    [HttpGet]
    [AllowAnonymous]
    public IActionResult Login(string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        return View();
    }
  ...
  }

The login view has an inject keyword like:

登录视图有一个注入关键字,如:

@model LoginViewModel
@inject SignInManager<ApplicationUser> SignInManager

@{
   ViewData["Title"] = "Log in";

 }

However, I don't see this in other views, and there's no trace of Ninject nor Unity in the app. What is this? a new amazing feature?

但是,我没有在其他视图中看到这一点,并且在应用程序中没有Ninject和Unity的痕迹。这是什么?一个新的惊人功能?

1 个解决方案

#1


1  

Asp.net 5 comes with built-in dependency injection and many features around it. In your example you are injecting an instance of SignInManager<ApplicationUser> named SignInManager into the razor view.

Asp.net 5带有内置的依赖注入和许多功能。在您的示例中,您将一个名为SignInManager的SignInManager 实例注入到剃刀视图中。

You can now use it just like you'd normally use the Model property: @SignInManager.Method()

您现在可以像使用Model属性一样使用它:@ SignInManager.Method()

See this article for more information: http://blog.tomasjansson.com/asp-net-5-ioc-and-dependency-injection/

有关更多信息,请参阅此文章:http://blog.tomasjansson.com/asp-net-5-ioc-and-dependency-injection/

#1


1  

Asp.net 5 comes with built-in dependency injection and many features around it. In your example you are injecting an instance of SignInManager<ApplicationUser> named SignInManager into the razor view.

Asp.net 5带有内置的依赖注入和许多功能。在您的示例中,您将一个名为SignInManager的SignInManager 实例注入到剃刀视图中。

You can now use it just like you'd normally use the Model property: @SignInManager.Method()

您现在可以像使用Model属性一样使用它:@ SignInManager.Method()

See this article for more information: http://blog.tomasjansson.com/asp-net-5-ioc-and-dependency-injection/

有关更多信息,请参阅此文章:http://blog.tomasjansson.com/asp-net-5-ioc-and-dependency-injection/