asp.net-core

Injecting services into views

Syntax#

  • @inject<NameOfService><Identifier>
  • @<Identifier>.Foo()
  • @inject <type> <name>

The @inject Directive

ASP.NET Core introduces the concept of dependency injection into Views via the @inject directive via the following syntax :

@inject <type> <name>

Example Usage

Adding this directive into your View will basically generate a property of the given type using the given name within your View using proper dependency injection as demonstrated in the example below :

@inject YourWidgetServiceClass WidgetService

<!-- This would call the service, which is already populated and output the results -->
There are <b>@WidgetService.GetWidgetCount()</b> Widgets here.

Required Configuration

Services that use dependency injection are still required to be registered within the ConfigureServices() method of the Startup.cs file and scoped accordingly :

public void ConfigureServices(IServiceCollection services)
{
     // Other stuff omitted for brevity 

     services.AddTransient<IWidgetService, WidgetService>();
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow