0

I have a class that inherits from DelegatingHandler. In one of the methods of that class I am trying to access a text files like so:

string filePath = HttpContext.Current.Server.MapPath("~/test.txt");

if (!string.IsNullOrWhiteSpace(filePath))
{
    using (StreamWriter w = File.AppendText(filePath))
    {
        w.WriteLine(string.Format("{0}|{1})", DateTime.Now, ex.ToString()));
    }
}

But I keep getting a null error on HttpContext.Current cannot be null

string filePath = HttpContext.Current.Server.MapPath("~/test.txt");

What could the issue be? I have made sure the file does exist at the site root?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
user1144596
  • 2,068
  • 8
  • 36
  • 56
  • I think it has something to do with [different threads](http://stackoverflow.com/questions/31419033/httpcontext-current-is-null-inside-identity-frameworks-methods). – diiN__________ Nov 07 '16 at 12:39
  • Have you given the administrator priviliges to your compiler ? – omerv2 Nov 07 '16 at 12:52

1 Answers1

1
string filePath = Server.MapPath("~/test.txt");
  • Use this Server.MapPath, it return the current full path of application
Jani Devang
  • 1,099
  • 12
  • 20