English 中文(简体)
How should I process the values of optional Query Strings?
原标题:

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!

最佳回答

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.

问题回答

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签