[英]How check if Request.QueryString is not empty
I have my own HandleErrorAttribute
, which reads data from Exception
and HttpContext
, insert them into template and send an e-mail to administrator. 我有自己的
HandleErrorAttribute
,它从Exception
和HttpContext
读取数据,将它们插入模板中并向管理员发送电子邮件。 It usually works fine, but it some cases it breaks, when trying to check if QueryString
is not empty. 它通常可以正常工作,但是在尝试检查
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 ]";
}
It breaks when checking condition ( NullReferenceException
), even if I can see in debug, that QueryString
is not null and the Keys.Count
is equal to 0. 即使我在调试中看到条件检查(
NullReferenceException
),它也会中断,即QueryString
不为null且Keys.Count
等于0。
What am I missing here? 我在这里想念什么? How to do a proper check for an empty
QueryString
? 如何对空的
QueryString
进行适当的检查?
Stacktrace: 堆栈跟踪:
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 exc, HttpContextBase context) w c:\Users\Marcin Bigoraj\Documents\Visual Studio 2012\Projects\project\Infrastructure\Attributes\ErrorHandlingAttribute.cs:wiersz 141
w project.Infrastructure.Attributes.ErrorHandlingAttribute.<>c__DisplayClass2.<OnException>b__1() w c:\Users\Marcin Bigoraj\Documents\Visual Studio 2012\Projects\project\Infrastructure\Attributes\ErrorHandlingAttribute.cs:wiersz 182
w System.Threading.Tasks.Task.InnerInvoke()
w System.Threading.Tasks.Task.Execute()
Check this code 检查此代码
if (Request.QueryString.Keys.Count > 0)
{
}
You can determine if there are any values in the QueryString by checking its count: 您可以通过检查QueryString的计数来确定QueryString中是否有任何值:
Request.QueryString.Count > 0;
It is sufficient. 足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.