Users Pricing

forum

Home Forums Why would HttpContext not contain a “Host” header? – MindStick

Why would HttpContext not contain a “Host” header?

Jayden Bell 2124 25 Aug 2014

In my MVC3 application I have a custom controller factory that has CreateController()method working as follows:

  public IController CreateController(RequestContext requestContext, string controllerName)
   {
       string host =requestContext.HttpContext.Request.Headers["Host"];
       if(!host.EndsWith( SomeHardcodedString ) ) { // FAILS HERE
           //some special action
       }
       //proceed with controller creation
   }

the problem is host is null sometimes - I see NullReferenceException for some requests and the exception stack trace points exactly at that line.

Why would null be retrieved here? How do I handle such cases?


1 Answers

Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md