Home > Blockchain >  IwebHostEnvironment interface
IwebHostEnvironment interface

Time:03-04

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

CodePudding user response:

Agree with @Nkosi, you can refer to enter image description here

  • Related