How do you test your Request.QueryString[] variables?

前端 未结 11 2177
梦如初夏
梦如初夏 2020-11-29 16:05

I frequently make use of Request.QueryString[] variables.

In my Page_load I often do things like:

       int id = -1;

             


        
11条回答
  •  天涯浪人
    2020-11-29 16:38

    Well for one thing use int.TryParse instead...

    int id;
    if (!int.TryParse(Request.QueryString["id"], out id))
    {
        id = -1;
    }
    

    That assumes that "not present" should have the same result as "not an integer" of course.

    EDIT: In other cases, when you're going to use request parameters as strings anyway, I think it's definitely a good idea to validate that they're present.

提交回复
热议问题