0

A simple question but I'd rather ask since I had problem with it two times.

I am trying to retrieve hostname from the url in ASP.NET project. The code is actually not in a web page but in a class (part of the domain). System.Web is included in the headers. When I try to use Request.QueryString it is not recognized. Even worse if I try HttpContext.Current, I get this error

'System.Web.HttpContext.Current' is null

Here is my code

using System.Web;

public class MyNightlyJob : AbstractJob
{
    public override void ExecuteJob(IJobExecutionContext context)
    {
          HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; // does not work
          Request.ServerVariables["HTTP_HOST"]; // this does not work also
    }
}

What am I missing? Note that my question is actually about Request.ServerVariables but if one works, the other will work too.

Note that Request.ServerVariable is not recognized in the code at all. HttpContext.Current is recognized but I get run time error for that.

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136

1 Answers1

0

You need to check if HttpContext.Current is a null. If it is null then its not in the right context to be retrieved.

If you can modify the function, I would pass the HttpContext.Current as a variable or pass the host as a variable.

manit
  • 604
  • 7
  • 11
  • That's a good idea but it is a big code and I myself does not want to make this change. Yes, `HttpContext.Current` is returned null in my code. – TheTechGuy Oct 03 '13 at 14:36