Search code examples
asp.net-corehttpcontext

What is the .NET Core alternative to HttpContext.Current?


What is the alternative to HttpContext.Current in .NET Core?

In .NET Framework, this code would look like:

ClsLog.WriteLog(HttpContext.Current, GetType(), "GetManualWeightData", ActionType.Action_Read, model.UID, model, clsResponse, clsResponse.ErrorMsg, model.LogCode);

How would I implement this in .NET Core?

Here is my ClsLog code:

public class ClsLog

{
    public static void WriteLog(HttpContext current, Type type, string strMethodName, string strActionType, string strUID, object strParam, object strResponse, string strErrorMsg = "", string strLogCode = "")
    {
        string strLocalIP = "";
        string strBrowser = "";
        string strMemID = "";
        string strCustomerID = "";

        if (current != null)
        {
            //strLocalIP = current.Session("LocalIP"] + "";
            //strBrowser = current.Session["BrowserInfo"] + "";
            //strMemID = current.Session["LogID"] + "";
            //strCustomerID = current.Session["CustomerID"] + "";
        }
        try
        {
            Task.Run(() => ClsWriteLog.getInstance().WriteLogServer(new ClsVariableAppLog("AppAPILog", strLocalIP, strBrowser, strMemID, strCustomerID, type.Module.Name, strMethodName, strUID, strActionType, type.FullName, strErrorMsg, strParam, strResponse, strLogCode)));
            //await ClsWriteLog.getInstance().WriteLogServer(new ClsVariableAppLog("AppAPILog", strLocalIP, strBrowser, strMemID, strCustomerID, type.Module.Name, strMethodName, strUID, strActionType, type.FullName, strErrorMsg, strParam, strResponse));
        }
        catch (Exception ex)
        {
            Trace.WriteLine("LogError - " + ex.Message);
        }
    }
}

Solution

  • Inject the IHttpContextAccessor type and read its HttpContext property.