相关疑难解决方法(0)

使用UrlHelper.Action方法生成URL时出现空引用异常

出于某种原因,当某些机器人访问该站点时,使用该UrlHelper.Action方法生成一个url 会引发一个null异常System.Web.HttpServerVarsCollection.Get.我做了一些调试,调用堆栈起源于尝试从HttpContextBase.Request.ServerVariables集合中获取'HTTP_X_ORIGINAL_URL' .

如果我直接从浏览器访问同一个地址 - 没问题.该页面是服务器,不记录任何错误.它似乎只发生在机器人访问时.

不确定它是否相关,但该网站刚刚迁移到IIS 7.5.仍在集成模式下使用.NET 2.0.

通过Reflector查看反转的代码,在Get方法中可以直接发生null异常的唯一地方是调用this._request.FetchServerVariables.好像没有正确设置完整的请求.

有没有其他人遇到这个问题或发现了一个解决方法?为什么在机器人访问时请求的设置不同?

更新:一些额外的调试表明HttpServerVarsCollection是Disposed,以及它的父HttpRequest对象.现在的问题是 - 如何在请求完成之前公开HttpContext.Current返回的Request对象?

HttpServerVarsCollection.Get方法

public override string Get(string name)
{
    if (!this._populated)
    {
        string simpleServerVar = this.GetSimpleServerVar(name);
        if (simpleServerVar != null)
        {
            return simpleServerVar;
        }
        this.Populate();
    }
    if (this._iis7workerRequest == null)
    {
        return this.GetServerVar(base.BaseGet(name));
    }
    string serverVar = this.GetServerVar(base.BaseGet(name));
    if (string.IsNullOrEmpty(serverVar))
    {
        // Only place null reference can happen
        serverVar = this._request.FetchServerVariable(name);
    }
    return serverVar;
}
Run Code Online (Sandbox Code Playgroud)

完整堆栈跟踪

NullReferenceException: …
Run Code Online (Sandbox Code Playgroud)

.net nullreferenceexception asp.net-mvc-2

17
推荐指数
1
解决办法
4329
查看次数