博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
Map集合的四种遍历方式
查看>>
MySQL监控项一些指标
查看>>
Thinkpad T430s NVS5400M Ubuntu 12.04安装
查看>>
定时拍照功能
查看>>
[Unity3d]SecurityException报错解决办法
查看>>
SCVMM创建Linux虚拟机模版
查看>>
添加 Pool Member - 每天5分钟玩转 OpenStack(123)
查看>>
NSDECODER v1.0
查看>>
游侠原创:vmware下android-x86-4.4-RC1体验
查看>>
OpenMNS--管理网络的绝好工具
查看>>
ORACLE LINUX 6.1安装过程
查看>>
iPhone/Mac Objective-C内存管理原理
查看>>
整理Silverlight资源列表(三)-SL实际运用案例
查看>>
02-BGP选路原则和属性详解--weight
查看>>
7.[数据结构和算法分析笔记]词典 Dictionary
查看>>
CCNP精粹系列之八----帧中继全网拓扑试验配置
查看>>
Lync升级S4B秘籍,So Easy!!!
查看>>
SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例...
查看>>
android用户界面-组件Widget-进度条ProgressBar
查看>>
猜字谜小游戏编程
查看>>