内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/89190459
版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/89190459
我又搞回笃NET啦!java之路真是命运多舛,好事多磨。不过,也许我已经进入无招胜有招,博取众家之长、融会贯通的地步了。
对于WebApi,今天又有了一些新的了解。
话说,Get请求方式,参数会附在Url后面,称为QueryString,传递给服务器;而POST方式,则将参数放在消息体内。采用QueryString的话,简单,方便,但只适合参数比较少的情况;但有的时候,需要传递比较多、比较复杂的参数,比如,组合条件查询。
组合条件查询,条件会很多,通常会用一个实体类来封装,传递给服务器。用post方式肯定是可以的,驾轻就熟。问题是,RESTful原则,是将一切抽象成资源,对资源的不同请求方式,代表了对资源的不同操作。按道理,post难道不是代表插入吗?为啥查询也要用到post?
GET方式下,也可以将参数提交给服务器?但查来查去,ajax好像可以;但服务器端,使用WebClient就没有找到现成的例子,不知道该咋整。只好用最原始的方法,将实体类转化成QueryString,附在地址后面了。有两个问题:
1、实体类如何转换成QueryString这种键值对格式?
2、服务器端如何提取?
一、实体类如何转换成QueryString这种键值对格式?
叫键值对可能不够专业,叫 NameValueCollection
?
很遗憾,也没找到啥现成的例子。
最后祭出反射,来拼装QueryString
二、服务器端如何提取QueryString里参数,自动变成一个实体类?
参数前面标注[FromUri]特性
上代码。
实体类:
namespace BaseLT.Core.Contract { public class Request { public Request(); public int Top { set; } public int PageSize { get; set; } public int PageIndex { get; set; } public string OrderBy { get; set; } public int SortState { get; set; } public bool CompareObject<T>(T obj1, T obj2); public void ExtjsInit(); } }
WebApi服务器端:
public class TankController : ApiController { [HttpGet] [Route("api/tank/matters/public/{id=0}")] public IEnumerable<Matter> Get(int id,[FromUri]Request req) { return do sth; } }
客户端:
[TestMethod] public void TestTankApi() { string url = "http://localhost/ybjzuser.api/api/tank/matters/public/"; url += getQueryString(new Request() { PageIndex = 1, PageSize = 100 }); string re; using (WebClient webClient = new WebClient()) { webClient.Encoding = Encoding.GetEncoding("utf-8"); re = webClient.DownloadString(url); } Assert.AreNotEqual(null, re); Console.WriteLine(re); } static string getQueryString(Request req) { StringBuilder query = new StringBuilder("?"); PropertyInfo[] propertys = req.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (pi.CanRead) { query.Append($@"{pi.Name}={pi.GetValue(req)}&"); } } return query.ToString(); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- EF架构~FluentValidation实体检验与实体分离了
- 表单 – 如何使用实体列表(CRUD)从模板中删除实体?
- MyBatis Generator配置文件--指定生成实体类使用实际的表列名作为实体类的属性名
- 命名实体识别技术
- XML实体扩展攻击
- [DeepNLP] 初识命名实体识别
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming Amazon Web Services
James Murty / O'Reilly Media / 2008-3-25 / USD 49.99
Building on the success of its storefront and fulfillment services, Amazon now allows businesses to "rent" computing power, data storage and bandwidth on its vast network platform. This book demonstra......一起来看看 《Programming Amazon Web Services》 这本书的介绍吧!