为什么 HttpContext 不包含“Host”?标题?
在我的 MVC3 应用程序中,我有一个自定义控制器工厂,它的 CreateController()
方法工作如下:
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
}
问题是 host
有时为 null - 我看到 NullReferenceException 对于某些请求,异常堆栈跟踪恰好指向该行。
为什么会在这里检索 null
?我该如何处理此类情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 string host = requestContext.HttpContext.Request.Url.Host;
Use
string host = requestContext.HttpContext.Request.Url.Host;
要处理它,您可能需要尝试以下操作:
To handle it, you might want to try something like: