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

你可能感兴趣的文章
取周期性字符串中的其中一个
查看>>
d3.js ----面积图表
查看>>
Zepto这样操作元素属性
查看>>
30-seconds-code——Object
查看>>
pyspark底层浅析
查看>>
【设计模式】组合模式之神经网络应用
查看>>
Jenkins系统搭建及常见操作
查看>>
SQL Server 2012自动异地备份
查看>>
Ubuntu 下 SVN 多版本库的搭建
查看>>
CSS选择器
查看>>
一款简单到极致的 React 数据流框架——Refast
查看>>
ribbon的ServerListRefreshInterval
查看>>
Android我还可以相信你多少系列文章二之音视频播放
查看>>
Adaptive Execution让Spark SQL更高效更好用
查看>>
快手服务治理平台KESS的设计理念和实战
查看>>
微软发布Azure Cosmos DB产品以及新的物联网解决方案
查看>>
与Bob McWhirter的问答:WildFly Swarm更名为Thorntail项目
查看>>
Java 11正式发布,新特性解读
查看>>
《Fit for Purpose》作者访谈录
查看>>
与Brian Goetz聊Java的数据类
查看>>