IwebHostEnvironment interface

Viewed 290

i have used IwebHostEnvironment in my code but i have a question .. IWebHostEnvironment is an interface used to provide info about hosting & can get data about path from it why i take an instance from it and inject it like i was inject a _db without register it in ConfigureServices method like i register Db context?!

 public class ProductController : Controller
{
    private readonly App_DbContext _db;
    private readonly IWebHostEnvironment _webHostEnvironment;
    public ProductController(App_DbContext db,IWebHostEnvironment webHostEnvironment)
    {
        _db = db;
        _webHostEnvironment = webHostEnvironment;
    }
    public IActionResult Index()
    {
        IEnumerable<Product> products= _db.Product;
        return View(products);
       
    }

this is configure services Method

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<App_DbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddControllersWithViews();
    }
1 Answers

Agree with @Nkosi, you can refer to this section, IWebHostEnvironment is one of the framework-registered services.

enter image description here

Related