简体   繁体   中英

IwebHostEnvironment interface

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();
    }

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM