我应该如何处理可选查询字符串的值?
我对 C# 和 ASP.NET 还很陌生,所以请耐心听我讲这一点。我正在设置一个从 url 接收查询字符串的页面。然后,它将这些字符串传递给另一个方法(实际上是在另一个类中),该方法根据查询字符串的值继续执行许多操作。
一般结构看起来像这样,其中 DoSomething()
实际上是另一个类的一部分,该类将被许多不同的页面使用:
pretected void Page_Load (object sender, EventArgs e)
{
DoSomething (Request.QueryString["name"]);
}
public void DoSomething (string UrlVariable)
{
// if UrlVariable isn't set, initialize it to some value
// do stuff with UrlVariable
}
这就是我想知道的:
- 如果查询字符串“name”是url 中没有定义,Request.QueryString 返回什么?空字符串?
空
? - 如果它返回
null
,如果我尝试将null
传递给需要字符串的方法,会发生什么情况?整个程序是否会崩溃,或者我可以在 DoSomething() 方法中检查null
(收到空字符串后)吗?
“名称”是可选的,因此如果用户没有设置它,我想检测它并将其初始化为某个默认值。不过,如果可能的话,我想将任何验证放在 DoSomething() 中,而不是在请求该字符串的每个页面上进行检查。
提前致谢!
I'm pretty new to C# and ASP.NET, so bear with me on this one. I'm setting up a page that receives query strings from the url. It then passes these strings to another method (in another class, actually), which goes on to do lots of things depending on the value of the query string.
The general structure looks something like this, where DoSomething()
is actually part of another class that will be used by lots of different pages:
pretected void Page_Load (object sender, EventArgs e)
{
DoSomething (Request.QueryString["name"]);
}
public void DoSomething (string UrlVariable)
{
// if UrlVariable isn't set, initialize it to some value
// do stuff with UrlVariable
}
Here's what I'm wondering:
- If the query string "name" isn't defined in the url, what does Request.QueryString return? an empty string?
null
? - If it returns
null
, what happens if I try to passnull
to a method that is expecting a string? Does the whole program fall apart, or can I check fornull
inside the DoSomething() method (after receiving the null string)?
The "name" is optional, so if the user doesn't set it, I'd like to detect that and initialize it to some default value. If possible, though, I'd like to put any validation inside DoSomething(), instead of doing the check on every page that requests the string.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果查询字符串中没有定义“name”,它确实会返回
null
。如果您尝试将 null 传递给方法(例如string.Format
),您通常会收到ArgumentNullException
。处理此问题的一个好方法是在将值传递到 DoSomething 时使用空合并运算符,例如:由于
DoSomething
是您的,因此您可以选择将此逻辑放入在那里(取决于它是否被重用),以保持代码干燥。If "name" isn't defined in the query string, it will indeed return
null
. If you attempt to pass null to a method (likestring.Format
for example), you will generally get aArgumentNullException
. A good way to handle this is to use a null-coalescing operator when passing a value intoDoSomething
for example:Since
DoSomething
is yours though, you could choose to put this logic in there (depending on if it's reused or not) in order to keep your code DRY.1.) 它应该返回 null
2.) 这是可以的,因为字符串可以为空;话虽如此,正如您指出的那样,您需要从函数的开头检查它是否为空
1.) It should return null
2.) this is OK because strings are nullable; that being said you would want to check for it being null from the start of the function as you point out