如何在ASP.NET Core中使用HttpContext.Current

时间:2018-07-02 12:28:47

标签: c# asp.net-core .net-core

我需要在HttpContext.Current函数的类HostingEnvironment中获得EmployeeDataAccessLayerAddEmployee。我已经在下面的代码中编写了代码,现在我遇到一个问题,该如何在我的AddEmployee中调用/使用函数controller

现在,由于我分别使用参数constructorsIHttpContextAccessor创建了两个新的IHostingEnvironment,这给我带来了麻烦,所以我没有找到使用它的正确方法。

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

我正在关注this文章。

2 个答案:

答案 0 :(得分:1)

很可能您尚未将控制器配置为需要EmployeeDataAccessLayer实例。

请确保将EmployeeDataAccessLayer注册为依赖项,如您在链接的文章中所述。然后,您的控制器应将EmployeeDataAccessLayer用作构造函数参数,您将其存储为只读字段,并在控制器操作中使用它。然后,您应该看到EmployeeDataAccessLayer具有提供的IHttpContextAccessor实例。

查看一个更完整的示例,因为您链接的示例不完整(例如,来自Microsoft的this one)。

作为一个补充说明,在您的EmployeeDataAccessLayer中,如果可能的话,您可能不应该要求IHttpContext依赖项,因为其他人在注释中已经提到。

答案 1 :(得分:1)

尝试使用如下界面:

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

另一种丑陋的方法(使用服务定位器):

如果您不希望使用DI和构造函数,则可以按以下方式使用服务定位器:

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{}
        }
}