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

你可能感兴趣的文章
JDBC连接MySQL数据库及演示样例
查看>>
【WP8.1开发】基于应用的联系人存储
查看>>
AI新时代-教你使用python+Opencv完成人脸解锁(附源码)
查看>>
MongoDB ( 三 )高级_状态返回和安全
查看>>
基于 Netty 的可插拔业务通信协议的实现「1」协议描述及基本消息对象设计
查看>>
NodeJS介绍以及开发微信公众号Example
查看>>
新时代前端的自我修养—2017 D2主题分享记录及我的思考
查看>>
java并发编程学习14--CompletableFuture(一)
查看>>
ES6语法之Symbol
查看>>
取周期性字符串中的其中一个
查看>>
d3.js ----面积图表
查看>>
Zepto这样操作元素属性
查看>>
30-seconds-code——Object
查看>>
pyspark底层浅析
查看>>
【设计模式】组合模式之神经网络应用
查看>>
Jenkins系统搭建及常见操作
查看>>
SQL Server 2012自动异地备份
查看>>
Ubuntu 下 SVN 多版本库的搭建
查看>>
CSS选择器
查看>>
一款简单到极致的 React 数据流框架——Refast
查看>>