CodeDigest.Com Logo

View Injection,Action Injection and Resolving Framework Services using Asp.Net Core MVC DI Container

Tagged as: Asp.Net Core Asp.Net Core MVC Posted By

My previous article, Using In-built Dependency Injection Framework in Asp.Net Core MVC gave an introduction about inbuilt Dependency Injection framework and the basic usages of the inbuilt framework. We also understood how to manage the dependency objects lifetime using various extension methods available in IServiceCollection in the previous article.

Moving forward, let’s see some more usages of the inbuilt DI framework like view injection, action injection and other useful features of in-built DI container in this article.

View Injection

Similar to using inbuilt DI framework for resolving constructor dependency, Asp.Net Core also allows injecting dependencies to razor views. Asp.Net Core allows injecting dependencies using @inject directive. For example, consider we have a service (AnalyticsService) that returns the site usage statistics which we need to display in every page. To do this, we need to first use @inject directive to inject the service into the view, layout page in our case.


@inject DIDemo.Services.SiteAnalyticsServices AnalyticsService


We can now access the public properties, methods using the alias we used in @inject directive. Code below.


<div class="pull-right bg-primary">
    <dl class="dl-horizontal">
        <dt>No of Users Online:</dt>
        <dd>@AnalyticsService.UsersOnline </dd>
        <dt>Top Viewed Content:</dt>
        <dd> @AnalyticsService.TopViewContent </dd>

For the inbuilt framework to inject an instance of AnalyticsServices object, we need to register this as dependency in Startup class as seen below.


public void ConfigureServices(IServiceCollection services)
    // Add framework services.
    //Removed for brevity


One good use of view injection is to inject the services that provides data source for the dropdownlist and listbox controls. This helps to manage the list controls data source items from a centralized service.

Action Injection

Similar to injecting dependencies to views, we can also inject dependencies to action methods using [FromServices] attribute like below.

public IActionResult Index([FromServices]IPostRepository repo)


     return View();




The service IPostRepository should be registered in Startup class like below.


public void ConfigureServices(IServiceCollection services)
    // Add framework services.
    //Removed for brevity
    services.AddTransient<IPostRepository, PostRepository>();

Manually Getting Dependent Object

At times, we may require an instance of the registered dependent object manually instantiated for us. The HttpContext object exposes all the registered services in Startup method through a property called RequestServices. We can call GetService() method to manually request an instance of the registered object like below.


IPostRepository repository = HttpContext.RequestServices.GetService<IPostRepository>();


Include Microsoft.Extensions.DependencyInjection namespace for the above code to work.

Resolving In-built Framework Services

As mentioned in my previous article, the inbuilt DI framework is also used by Asp.Net Core and MVC framework to register services. There are many inbuilt framework services like IHttpContextAccessor, IUrlHelperFactory, IAntiforgery etc. are added by the framework. For instance, the extension method services.AddMvc() will add all the MVC related services into the container. This means we can get the implementation objects of all these services in our classes as dependency. For example, the below custom tag helper class has defined IHttpContextAccessor, IAntiforgery as dependency. The service provider will now provide the implementation object for these services when custom tag helper class is instantiated.


public class LoginStatusTagHelper : TagHelper
    UserManager<ApplicationUser> UserManager;
    IHttpContextAccessor HttpContextAccess;
    IAntiforgery Antiforgery;

    public LoginStatusTagHelper(IHttpContextAccessor httpContext, UserManager<ApplicationUser> userManager, IAntiforgery antiforgery)
        HttpContextAccess = httpContext;
        UserManager = userManager;
        Antiforgery = antiforgery;


Note – You can view the list services including the framework services added in the IServiceCollection by inserting a break point at the closing brace( } ) of ConfigureServices(IServiceCollection services) method and enabling the QuickWatch window. Refer the below image.

Happy Coding!!

Further Reading

  1. Read Difference Between Asp.Net and Asp.Net Core for a side by side difference.
  2. To know more about the new features in Asp.Net Core, read Breaking Changes and New Features of Asp.Net Core MVC.