我应该如何处理可选查询字符串的值?

发布于 2024-09-30 19:40:04 字数 843 浏览 3 评论 0原文

我对 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
}

这就是我想知道的:

  1. 如果查询字符串“name”是url 中没有定义,Request.QueryString 返回什么?空字符串?
  2. 如果它返回 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:

  1. If the query string "name" isn't defined in the url, what does Request.QueryString return? an empty string? null?
  2. If it returns null, what happens if I try to pass null to a method that is expecting a string? Does the whole program fall apart, or can I check for null 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-10-07 19:40:04

如果查询字符串中没有定义“name”,它确实会返回null。如果您尝试将 null 传递给方法(例如 string.Format),您通常会收到 ArgumentNullException。处理此问题的一个好方法是在将值传递到 DoSomething 时使用空合并运算符,例如:

DoSomething (Request.QueryString["name"] ?? "MyDefaultString");

由于 DoSomething 是您的,因此您可以选择将此逻辑放入在那里(取决于它是否被重用),以保持代码干燥。

If "name" isn't defined in the query string, it will indeed return null. If you attempt to pass null to a method (like string.Format for example), you will generally get a ArgumentNullException. A good way to handle this is to use a null-coalescing operator when passing a value into DoSomething for example:

DoSomething (Request.QueryString["name"] ?? "MyDefaultString");

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.

筑梦 2024-10-07 19:40:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文