HttpContext.Current.Server.MapPath Error: Object reference not set to an instance of an object

Corobori
 
on Feb 18, 2021 10:33 PM
2519 Views

I have got a code which, I believe, has been working for ages but it suddenly comes up with an error when referencing the Server.MapPath in a class.

I am using HttpContext.Current.Server.MapPath but now I seem to get an error saying "Object reference not set to an instance of an object"

Catch ex As Exception
    Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath("../App_Data/") & "logCopiarACP_" & pEm_Id & ".txt")
        Log("CopiarPropiedades2015_Videos " & ex.Message, w)
    End Using

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 18, 2021 11:07 PM

Hi Corobori,

Try using HttpRuntime.AppDomainAppPath instead and check.

Catch ex As Exception
    Using w As StreamWriter = File.AppendText(Path.Combine(HttpRuntime.AppDomainAppPath, "../App_Data/logCopiarACP_", pEm_Id, ".txt"))
        Log("CopiarPropiedades2015_Videos " & ex.Message, w)
    End Using

Reference

https://stackoverflow.com/questions/6861368/httpcontext-current-server-mappath-object-reference-not-set-to-an-instance-of-an/35941701