Sunil Kumar
Sunil Kumar

Reputation: 949

How to use HttpContext.Current in ASP.NET Core

I need to get HttpContext.Current and HostingEnvironment in a class EmployeeDataAccessLayer in AddEmployee function. I have written below code now I am facing a problem that how can I call/use the function AddEmployee in my controller.

Now, as I have created 2 new constructors with parameters IHttpContextAccessor and IHostingEnvironment respectively causing me problem, I am not getting a proper way to use it.

    public class EmployeeDataAccessLayer 
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            private IHostingEnvironment _hostingEnvironment;        

            public EmployeeDataAccessLayer(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
            public EmployeeDataAccessLayer(IHostingEnvironment environment)
            {
                _hostingEnvironment = environment;
            }  
 public void AddEmployee(TblEmployee employee)
        {
            try
            {              
                string folderName = "UploadFile/";
                string sPath = "";
                sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);

                var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
}   

I am following this article.

Upvotes: 1

Views: 7185

Answers (2)

Anupam Singh
Anupam Singh

Reputation: 1166

Try using interface as follows :

 //controller
 public class HomeController
    {
      private readonly IDataAccess _dataAccess;

      public HomeController(IDataAccess dataAccess)
      {
        _dataAccess = dataAccess;
      }

      [HttpPost]
      public ActionResult Index(TblEmployee employee)
      {
       _dataAccess.AddEmployee(employee);
        return View();
      }
    }

 // Startup
 public void ConfigureServices(IServiceCollection services)
 {
     // add dependency
     services.AddScoped<IDataAccess, EmployeeDataAccessLayer>();
     services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
 }

 // Data Access Impl
 public class EmployeeDataAccessLayer : IDataAccess 
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            private IHostingEnvironment _hostingEnvironment;        

            public EmployeeDataAccessLayer(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
            public EmployeeDataAccessLayer(IHostingEnvironment environment)
            {
                _hostingEnvironment = environment;
            }  
 public void AddEmployee(TblEmployee employee)
        {
            try
            {              
                string folderName = "UploadFile/";
                string sPath = "";
                sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);

                var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
           catch{}
      }  
 }
// interface
public interface  IDataAccess
{
 void AddEmployee(TblEmployee employee);
}

Another ugly approach (Using service locator):

if you don't want DI and constructor, you can use service locator as follows:

public static class MyServiceLocator
{
    public static IServiceProvider Instance { get; set; }
}

public void Configure(IApplicationBuilder app)
{
    MyServiceLocator.Instance = app.ApplicationServices;
}


 // Data Access
 public class EmployeeDataAccessLayer  
        {         

        public void AddEmployee(TblEmployee employee)
        {
            try
            {      

                IHttpContextAccessor httpContextAccessor =MyServiceLocator.Instance.GetService<IHttpContextAccessor>();
                IHostingEnvironment hostingEnvironment=MyServiceLocator.Instance.GetService<IHostingEnvironment>();; 


                string folderName = "UploadFile/";
                string sPath = "";
                sPath = Path.Combine(_hostingEnvironment.WebRootPath, "~/" + folderName);

                var hfc = _httpContextAccessor.HttpContext.Request.Form.Files;
           } 
          catch{}
        }
}   

Upvotes: 1

Dan
Dan

Reputation: 1013

Most likely you haven't configured your controller to require an instance of EmployeeDataAccessLayer.

Be sure to register the EmployeeDataAccessLayer as a dependency like in the article you linked. Then your controller should take EmployeeDataAccessLayer as a constructor argument, you will store that as a readonly field and use it in your controller action. Then you should see that EmployeeDataAccessLayer has an instance of IHttpContextAccessor provided.

See a more complete example as the one you link is not complete(e.g. this one from microsoft).

As a side note, in your EmployeeDataAccessLayer you probably should not require the IHttpContext dependency if possible as others have mentioned in comments.

Upvotes: 1

Related Questions