如何Request客户端的传值的Data
我们在做B/S的项目,客户端向服务端传值的时候,一般都是request接受。
Request常用三个接受方式为:Request.QueryString,Request.Form,Request.Params
它们之间关系,很简单 ,我就不列举了。
我就贴下 一个我封装的示例代码:
class Program
{ static void Main(string[] args)
{
//First Method
string _id = string.Empty;
string _prodctName = string.Empty;
int _Price = ;
//IsNullOrWhiteSpace 代替IsNullOrEmpty
if (!string.IsNullOrWhiteSpace(HttpContext.Current.Request.QueryString["id"]))
{
_id = HttpContext.Current.Request.QueryString["id"];
}
if (!string.IsNullOrWhiteSpace(HttpContext.Current.Request.QueryString["prodctName"]))
{
_prodctName = HttpContext.Current.Request.QueryString["prodctName"];
}
if (!string.IsNullOrWhiteSpace(HttpContext.Current.Request.Params["Price"]))
{
_Price = int.Parse(HttpContext.Current.Request.QueryString["Price"]); //很危险哦 很容易就报错
} //two Method
// 我最常用的方法 利用委托和扩展方法 进行组合链式
//这里可以封装到一个方法里
Func<string, string> GetRequest = (string s) =>
{
var str=HttpContext.Current.Request.QueryString[s];
if (!string.IsNullOrWhiteSpace(str))
{
return str;
}
else
return "";
}; _id = GetRequest("id");
_prodctName = GetRequest("prodctName");
_Price = GetRequest("price").FormatInt(); //third Method
//这里不做代码展示 一般用AJAX上 大致步骤如下
//1.前台用做一个JSON对象传递到后台,拒绝细粒度传值,细粒度一般在传参少于三个以下, 超过三个都用粗粒度传值
//2.后台用json.net 进行反格式到model上 //four Method
//这里是我现在真打算运用项目中,性能和可行性还在测试中,等成功 我就更新出来
//希望能抛砖引玉,谢谢
}
} public static class FormatData
{
public static string FormatString(this string s)
{
return s.Replace("猴子吃香蕉","猴子吃菠萝");
}
public static int FormatInt(this string s)
{
//默认-100 如果-100表示格式化出错 约定一个准则就可以
var _tempData = -;
int.TryParse(s, out _tempData);
return _tempData;
}
//后面还有很多 如 日期 浮点型 过滤SQL非法字符等等
}
如何Request客户端的传值的Data的更多相关文章
- vue项目 Request Payload改成Form Data
vue项目中提交表单时,请求参数是Request Payload时在main.js中加 axios.defaults.headers.post['Content-Type'] = 'applicati ...
- JQuery ajax url传值与data传值的区别
url传中文,乱码,即便charset为 UTF-8, $.ajax({ type: "POST", cache: false, url: "/Prod ...
- Vue父组件向子组件传值以及data和props的区别
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/xukongjing1/article/ ...
- 第六章 组件 63 组件传值-父组件向子组件传值和data与props的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Netty源码分析之客户端启动过程
一.先来看一下客户端示例代码. public class NettyClientTest { public void connect(int port, String host) throws Exc ...
- HttpClient实现客户端与服务器的通信
本篇主要讲解了利用HttpClient实现 windows主机与linux服务器的通信与传递数据 HttpClient代码,服务器端配置 系统和安装软件 1)ubuntu 14.04 64位系统 2) ...
- python中urllib, urllib2,urllib3, httplib,httplib2, request的区别
permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...
- 开源中国iOS客户端学习
开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客 http://blog.csdn.net/column/details/xfzl-kykhd.html ...
- Request和Response详解
转自:http://zhidao.baidu.com/link?url=8BI0cjlcFdBSJKHTZlpo874eqtbTJoZfrh3miQgM_05RvSER8skPiBc1wSPZtXT8 ...
随机推荐
- SICP-Exercise 1.5
Exercise 1.5. Ben Bitdiddle has invented a test to determine whether the interpreterhe is faced wit ...
- 在java代码中用xslt处理xml文件
java处理xml文件 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- 六、从length和length()方法开始
首先你可以快速回答下面问题吗.当没有任何IDE的情况下,如何得到一个数组的长度,如何得到一个String的长度.我问这个了很对不同水平的开发者:初级的中级的.他们不能快速正确的回答这个问题.当IDE提 ...
- js正则表达式判断一个字符串是否是正确的有数字和小数点组成的金钱形式和 判读数值类型的正则表达式
function checkRates(str){ var re = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*) ...
- OpenLdap 相关命令
相关命令: |-slapd 目录服务的主要程序|-slurpd 目录服务进行复制的程序|-slapadd 向目录中添加数据|-slapcat 把目录中的条目导出成ldif文件|-slapindex 重 ...
- 利用IT++搭建通信仿真平台
IT++ is a C++ library of mathematical, signal processing and communication classes and functions.也就是 ...
- VC中使用Matlab Engine出现"无法找到libeng.dll"的问题
VC中使用Matlab Engine出现"无法找到libeng.dll"的问题 本以为使这个原因 ,其实不是我2了 #include "engine.h" // ...
- 【WPF】ListBox无法滚动
问题:ListBox显示多个条目时,无法滚动,也不显示滚动条. 办法: 给ListBox控件加上ScrollViewer.VerticalScrollBarVisibility和ScrollViewe ...
- RP2833 FPGA对应串口标识
U41 FPGA-TXD0 /dev/ruart0 FPGA-RXD0 U40 FPGA-TXD1 /dev/ruart1 FPGA-RXD1
- netfiler/iptables
一. 什么是netfilter netfilter is a set of hooks inside the Linux kernel that allows kernel modules to re ...