[英]ASP.NET 5 MVC (Visual Studio 2015): a keyword called Inject
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. 我刚安装了visual studio 2015并玩了ASP.NET 5模板(MVC),我看到了一个名为inject
的关键字,它已在视图中使用过。 Is it for dependency injection, and if yes how does it work? 是依赖注入,如果是,它是如何工作的? Assuming the below code: 假设以下代码:
[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. 但是,我没有在其他视图中看到这一点,并且应用程序中没有Ninject和Unity的痕迹。 What is this? 这是什么? a new amazing feature? 一个新的惊人功能?
Asp.net 5 comes with built-in dependency injection and many features around it. Asp.net 5内置了依赖注入功能,并且附带了很多功能。 In your example you are injecting an instance of SignInManager<ApplicationUser>
named SignInManager
into the razor view. 在您的示例中,您将一个名为SignInManager
的SignInManager<ApplicationUser>
实例注入到剃刀视图中。
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.