---
title: "Why would HttpContext not contain a “Host” header?"  
description: "Why would HttpContext not contain a “Host” header?"  
author: "Jayden Bell"  
published: 2014-08-25  
updated: 2014-08-25  
canonical: https://www.mindstick.com/forum/2131/why-would-httpcontext-not-contain-a-host-header  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# Why would HttpContext not contain a “Host” header?

In my MVC3 [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) I have a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) [factory](https://www.mindstick.com/blog/304231/factory-vs-service-in-angularjs-which-one-better) that has CreateController()[method](https://www.mindstick.com/forum/166/webservice-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](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is host is null sometimes - I see [NullReferenceException](https://www.mindstick.com/forum/159752/what-is-a-nullreferenceexception-and-how-can-you-prevent-it-in-your-dot-net-core-application) for some requests and the [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) trace points exactly at that line.

Why would null be retrieved here? How do I [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) such cases?

## Replies

### Reply by Sumit Kesarwani

Hi jayden, try this:

Use string host = requestContext.HttpContext.Request.Url.Host;


---

Original Source: https://www.mindstick.com/forum/2131/why-would-httpcontext-not-contain-a-host-header

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
