博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET 1.1 中 QueryString 的安全获取写法
阅读量:6891 次
发布时间:2019-06-27

本文共 1120 字,大约阅读时间需要 3 分钟。

 1
ExpandedBlockStart.gif
ContractedBlock.gif
public
 
class
 Util 
dot.gif
{
 2ExpandedSubBlockStart.gifContractedSubBlock.gif  private Util() dot.gif{}
 3InBlock.gif
 4InBlock.gif  // 从 querystring 集合中安全的取得一个 string. (总是不会有 null,所以叫做 'Safe')
 5ExpandedSubBlockStart.gifContractedSubBlock.gif  public static string GetStringSafeFromQueryString(Page page, string key) dot.gif{
 6InBlock.gif    string value = page.Request.QueryString[key];
 7InBlock.gif    return (value == null? string.Empty : value;
 8ExpandedSubBlockEnd.gif  }
 9InBlock.gif  
10InBlock.gif  // 在上述基础上,实现几个常用类型的获取方法。
11ExpandedSubBlockStart.gifContractedSubBlock.gif  public static int GetInt32SafeFromQueryString(Page page, string key, int defaultValue) dot.gif{
12InBlock.gif    string value = GetStringSafeFromQueryString(page, key);
13InBlock.gif    int i = defaultValue;
14ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
15InBlock.gif      i = int.Parse(value);
16ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch dot.gif{}
17InBlock.gif    return i;
18ExpandedSubBlockEnd.gif  }
19InBlock.gif  // double 的实现
20InBlock.gif  public static double GetDoubleSafeFromQueryString(Page page,
21ExpandedSubBlockStart.gifContractedSubBlock.gif    string key, double defaultValue) dot.gif{
22InBlock.gif    string value = GetStringSafeFromQueryString(page, key);
23InBlock.gif    double d = defaultValue;
24ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
25InBlock.gif      d = double.Parse(value);
26ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 catch dot.gif{}
27InBlock.gif    return d;
28ExpandedSubBlockEnd.gif  }
29InBlock.gif  // 同理可以写出 float,  的实现
30ExpandedBlockEnd.gif}
在我的任何页面里面,要获取 querystring 的时候,只要这样就可以了:
比如我要获取一个 string:
1
None.gif
string
 name 
=
 Util.GetStringSafeFromQueryString(
this
"
name
"
);
2
ExpandedBlockStart.gifContractedBlock.gif
if
 (name.Length 
>
 
0
dot.gif
{
3InBlock.gif  // 进行正常的处理
4ExpandedBlockStart.gifContractedBlock.gif}
 
else
 
dot.gif
{
5InBlock.gif  // 不处理。
6ExpandedBlockEnd.gif}
获取 int:
int
 id 
=
 Util.GetInt32SafeFromQueryString(
this
"
id
"
0
);

转载地址:http://fgqbl.baihongyu.com/

你可能感兴趣的文章
shell历史命令记录功能
查看>>
kali linux软件源
查看>>
cocos2d_x在windows环境下的方向键支持
查看>>
Maven学习总结(11)——Maven Tomcat7自动部署
查看>>
zabbix安装界面报连接不到数据
查看>>
pjsip 同时使用多套音频设备
查看>>
DevOps:怎么实现源代码注释和系统文档的自动化更新?
查看>>
make 中的路径搜索(十二)
查看>>
zabbix agent 端主动注册
查看>>
初识Mysql(二)
查看>>
监控系统的状态
查看>>
Samba文件共享服务
查看>>
软件目录开发规范
查看>>
compute post expression
查看>>
C#中DataTable中的Compute方法使用收集
查看>>
Python——特殊属性与方法
查看>>
Python pip 报错
查看>>
POJ2187:Beauty Contest——题解
查看>>
第29件事 评估需求的8种方法
查看>>
安卓工程简介
查看>>