简体   繁体   English

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

[英]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. 在您的示例中,您将一个名为SignInManagerSignInManager<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.

相关问题 在Visual Studio 2015中的asp.net Core上安装MVC - Installing MVC on asp.net Core in visual Studio 2015 如何在Visual Studio 2015中创建ASP.NET MVC 4项目 - How to Create an ASP.NET MVC 4 project in visual studio 2015 如何在Visual Studio 2015中安装ASP.NET MVC 5 Razor - How to install ASP.NET MVC 5 Razor in Visual Studio 2015 Visual Studio 2015,ASP.NET 5,MVC 6,Angular 2.0 - 例外:必须定义令牌 - Visual Studio 2015, ASP.NET 5, MVC 6, Angular 2.0 - Exception: Token must be defined 如何在Visual Studio 2015中使用MSBuild在构建上部署ASP.NET MVC应用程序? - How to deploy an ASP.NET MVC application on build using MSBuild in Visual Studio 2015? ASP.Net MVC-5 / EF-6 sln在Visual Studio 2015中工作,在VS 2017中失败 - ASP.Net MVC-5/EF-6 sln works in Visual Studio 2015, fails in VS 2017 无法从IIS(内部服务器500)使用Visual Studio 2015运行简单ASP.NET MVC应用程序 - Cannot run Simple ASP.NET MVC Application with Visual Studio 2015 from IIS (Internal Server 500) 如何使用Visual Studio 2013/2015启动并运行ASP.NET MVC音乐存储? - How to get ASP.NET MVC Music Store up and running with Visual Studio 2013/2015? MSBuild编译ASP.NET MVC4应用程序-使用Visual Studio 2015设计 - MSBuild compiling ASP.NET MVC4 application - designed using Visual Studio 2015 如何将ASP.NET MVC 5 Web App部署到带有Visual Studio 2015 Enterprise的Apache(带有Mono)? - How to Deploy ASP.NET MVC 5 Web App to Apache w/ Mono w/ Visual Studio 2015 Enterprise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM