如何识别查询字符串名称是否包含空格? [英] How to identify that querystring name contain space?

查看:370
本文介绍了如何识别查询字符串名称是否包含空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ex: sitename/sample.aspx?username=''&userid=''

cs file

string Username = "";
if (Request.QueryString["username"] != null)      
{
    Username = Request.QueryString["username"].ToString();
}
string userid = "";
if (Request.QueryString["userid"] != null)      
{
   userid = Request.QueryString["userid"].ToString();
}

If I use/pass 'username' or 'Username',both get check it is not case sensative.But if user pass like following

Ex: sitename/sample.aspx?username =''&userid=''

I want to restict it.How I check that querystring name contain space?





我尝试过:





What I have tried:

string userid = "";
if (Request.QueryString["userid"] != null)      
{
   userid = Request.QueryString["userid"].ToString();
}

推荐答案

如果您尝试捕获任何仅限空格的查询字符串,您始终可以将查询字符串修剪为摆脱前导和尾随空格,然后在修剪后检查其长度,例如

If you are trying to catch any space-only query string, you can always trim the query string to get rid of leading and trailing spaces, then check for its length after trimming, e.g.
string userid = " ";
if(userid.Trim().Length == 0){
     Console.WriteLine("userid is empty");
}


可以帮助.........检查



Might help......... Check with

string username="";
if(!String.IsNullOrEmpty(Request.QueryString["username"]))
{
  username=Request.QueryString["username"];
}
else
{
  Response.Write("Query String is null");
}


这篇关于如何识别查询字符串名称是否包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆