Injecting a service into a view in MVC 6

 

Asp.net MVC 6 supports injecting service to view using a class.

Example :- When country combo box value get changed , region com

Only restriction is that class should be

  • Public
  • Non-nested
  • Non-Abstract

1. Create a Asp.Net Project in Visual Studio 2015

image

 

image

 

2. Create a folder name services and add a service class.

  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4. namespace ViewInjection.Services
  5. {
  6.     public class EmployeeService
  7.     {
  8.         public async Task<int> GetEmployeesOnLeaveCount()
  9.         {
  10.             return await Task.FromResult(5);
  11.         }
  12.  
  13.         public async Task<int> GetEmployeesOnSiteCount()
  14.         {
  15.             return await Task.FromResult(
  16.                 10);
  17.         }
  18.  
  19.         public async Task<int> GetEmployeesOnWorkCount()
  20.         {
  21.             return await Task.FromResult(97);
  22.         }
  23.     }
  24. }

 

2. Create a View to show Employee statistics and add the inject statement to the top of the file.

  1. @inject ViewInjection.Services.EmployeeService EmployeeService
  2. @{
  3.      ViewBag.Title = "Employee Page";
  4. }

3. Call the service as below

  1.  
  2. @inject ViewInjection.Services.EmployeeService EmployeeService
  3. @{
  4.      ViewBag.Title = "Employee Page";
  5. }
  6.  
  7. <h3>Employee Stats</h3>
  8. <ul>
  9.     <li>On Leave: @await EmployeeService.GetEmployeesOnLeaveCount()</li>
  10.     <li>On Site:@await EmployeeService.GetEmployeesOnSiteCount()</li>
  11.     <li>On Work:@await EmployeeService.GetEmployeesOnWorkCount()</li>
  12. </ul>

4. Register the EmployeeService class in the Startup.cs file.

  1. public void ConfigureServices(IServiceCollection services)
  2.       {
  3.           // Add EF services to the services container.
  4.           services.AddEntityFramework(Configuration)
  5.               .AddSqlServer()
  6.               .AddDbContext<ApplicationDbContext>();
  7.  
  8.           // Add Identity services to the services container.
  9.           services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration);
  10.  
  11.           // Add MVC services to the services container.
  12.           services.AddMvc();
  13.           
  14.           services.AddTransient<ViewInjection.Services.EmployeeService>();
  15.  
  16.       }

5. Add a action method in HomeController.cs

  1.  
  2. public IActionResult EmployeeStaticstics()
  3. {
  4.     return View();
  5. }

6.Run your code you will see the output as below.

image

Happy Coding !!

Comments

  1. Why when we remove the roles authorize does not work automatically, because I need to log out and then a login for work? The problem is that the roles are stored in the cookie. I have to find some solution to update the cookie. When do I remove a roles directly in the database the cookie is outdated. I think I have update the cookie to each User request. The example I'm using is on github https://github.com/aspnet/Musi...

    ReplyDelete

Post a Comment

Popular posts from this blog

Responsive Web Design

Affine Cipher in C#

Contract First Development in WCF 4.5