[SOLVED] how to use HttpContext.Current in net core 5 (no necromenssing)

Issue

before we use net 5 we use net framework.

// Net Framework

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

//

but now we use net 5 and net 5 cant use current…
how to instaed of "current"?

+


 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.

Answered By – Stephen Cleary

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *