I have a UserController and a BaseController with some functions and dependencies injected as below. The BaseController is shared between multiple controllers. But All the controllers do not need all the injected dependencies from the BaseController. Controller1 Needs dependencies D1 and D2 but Controller2 requires only D1 from BaseController. How can I achieve this?
In the below code, UserController does not require the injected webHostEnvironment from the base but I need to inject it as it present in BaseController else Visual Studio throws compiler error of missing dependency. Please let me know if additional details are required to resolve the issue.
UserController
using MyApp.Data;
using MyApp.Models;
using MyApp.Models.ViewModels;
using MyApp.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace MyApp.Controllers
{
[Authorize]
public class UserController : BaseController
{
private readonly ApplicationDbContext db;
private readonly ILogger<UserController> logger;
private readonly RoleManager<IdentityRole> roleManager;
private readonly IEmailSender emailSender;
public UserController( ApplicationDbContext db,
ILogger<UserController> logger,
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IEmailSender emailSender,
IWebHostEnvironment webHostEnvironment) : base (userManager, webHostEnvironment)
{
this.db = db;
this.logger = logger;
this.roleManager = roleManager;
this.emailSender = emailSender;
}
[HttpGet]
public IActionResult Index()
{
var userList = userManager.Users;
return View(userList);
}
[HttpGet]
public async Task<IActionResult> Read(string? id)
{
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
return RecordNotFound();
}
UserViewModel model = new()
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
UserName = user.UserName,
Email = user.Email,
PhoneNumber = user.PhoneNumber,
};
return View(model);
}
//And Some more Action Methods for the controller
}
}
BaseController
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Utilities
{
[Authorize]
public class BaseController : Controller
{
protected readonly UserManager<ApplicationUser> userManager;
protected readonly IWebHostEnvironment webHostEnvironment;
public BaseController( UserManager<ApplicationUser> userManager,
IWebHostEnvironment webHostEnvironment)
{
this.userManager = userManager;
this.webHostEnvironment = webHostEnvironment;
}
[HttpGet] [HttpPost]
public IActionResult RecordNotFound()
{
TempData[HelperStatic.ErrorMessage] = HelperStatic.recordNotFoundMsg;
return View("Index");
}
[HttpPost]
public IActionResult SaveNew()
{
TempData[HelperStatic.SuccessMessage] = HelperStatic.recordSavedMsg;
return RedirectToAction("Create");
}
//And Many other Reusable Methods for all controllers
}
}
using Microsoft.AspNetCore.Hosting;reference, the code works well on my side, check this screenshot (as we can see the code execute to the break point).nullas the dependency for theBaseControllerconstructor? It is unclear if the dependency is optional or required.BaseController. In case it is optional, you can usenullas the value. In case it is required, you have to use an actual instance of the dependency (or let it injected by some framework for you).