小编Mar*_*raj的帖子

如何检查Request.QueryString是否为空

我有我自己的HandleErrorAttribute,它从读取数据Exception,并HttpContext,将它们插入到模板和发送电子邮件管理员.它通常工作正常,但在某些情况下,它会在尝试检查是否QueryString为空时断开.

var requestParams = "";
if (context.Request.QueryString != null && context.Request.QueryString.Keys.Count > 0)
{
    foreach (String key in context.Request.QueryString.Keys)
    {
        requestParams += key + ": " + context.Request.QueryString[key] + "<br />";
    }
}
else
{
    requestParams = "[ no query string data ]";
}
Run Code Online (Sandbox Code Playgroud)

它在检查condition(NullReferenceException)时会中断,即使我在调试中可以看到,它QueryString不是null并且Keys.Count等于0.

我在这里错过了什么?如何正确检查空QueryString

堆栈跟踪:

w System.Web.Hosting.IIS7WorkerRequest.GetQueryStringPtr(Int32& length)
w System.Web.Hosting.IIS7WorkerRequest.GetQueryStringRawBytes()
w System.Web.HttpRequest.get_QueryStringBytes()
w System.Web.HttpRequest.FillInQueryStringCollection()
w System.Web.HttpRequest.EnsureQueryString()
w System.Web.HttpRequest.get_QueryString()
w System.Web.HttpRequestWrapper.get_QueryString()
w project.Infrastructure.Attributes.ErrorHandlingAttribute.BuildErrorEmail(Exception …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc

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

标签 统计

asp.net ×1

asp.net-mvc ×1

c# ×1