Reputation: 170549
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?
Upvotes: 6
Views: 1818
Reputation: 1280
To handle it, you might want to try something like:
var host = requestContext.HttpContext.Request.Url.Host;
if (host != null)
if(!host.EndsWith("SomeHardcodedString"))
else
// Handle it
Upvotes: 2
Reputation: 2583
Use string host = requestContext.HttpContext.Request.Url.Host;
Upvotes: 7