webapi post 请求多个参数
Some programmers are tring to get or post multiple parameters on a WebApi controller, then they will find it not easy to solve it clearly, actually, there is a simple and pragmatical solution if we use json and dynamic object.
1.HttpGet
We can append json string in the query string content, and parsed the json object in server side.
var data = {
UserID: "10",
UserName: "Long",
AppInstanceID: "100",
ProcessGUID: "BF1CC2EB-D9BD-45FD-BF87-939DD8FF9071"
};
var request = JSON.stringify(data);
request = encodeURIComponent(request);
//call ajax get method
ajaxGet:
...
url: "/ProductWebApi/api/Workflow/StartProcess?data=",
data: request,
...
[HttpGet]
public ResponseResult StartProcess()
{
dynamic queryJson = ParseHttpGetJson(Request.RequestUri.Query);
int appInstanceID = int.Parse(queryJson.AppInstanceID.Value);
Guid processGUID = Guid.Parse(queryJson.ProcessGUID.Value);
int userID = int.Parse(queryJson.UserID.Value);
string userName = queryJson.UserName.Value;
...
}
private dynamic ParseHttpGetJson(string query)
{
if (!string.IsNullOrEmpty(query))
{
try
{
var json = query.Substring(7, query.Length - 7); // the number 7 is for data=
json = System.Web.HttpUtility.UrlDecode(json);
dynamic queryJson = JsonConvert.DeserializeObject<dynamic>(json);
return queryJson;
}
catch (System.Exception e)
{
throw new ApplicationException("wrong json format in the query string!", e);
}
}
else
{
return null;
}
}
2.HttpPost
2.1 Simple Object
We passed json object with ajax, and parse it with dynamic object in server side. it works fine. This is sample code:
ajaxPost:
...
Content-Type: application/json,
data: {"name": "Jack", "age": "12"}
...
[HttpPost]
public string ParseJsonDynamic(dynamic data)
{
string name = data.name;
int age = data.age;
return name;
}
2.2 Complex Object
The complex object means that we can package list and dictionary object, and parse them with dynamic object in server side. We call this composite pattern.
var jsonData = {
"AppName":"SamplePrice",
"AppInstanceID":"100",
"ProcessGUID":"072af8c3-482a-4b1c-890b-685ce2fcc75d",
"UserID":"20",
"UserName":"Jack",
"NextActivityPerformers":{
"39c71004-d822-4c15-9ff2-94ca1068d745":
[{
"UserID":10,
"UserName":"Smith"
}]
}
}
//call ajax post method
ajaxPost:
...
Content-Type: application/json,
data: jsonData
...
[HttpPost]
public string ParseJsonComplex(dynamic data)
{
//compsite object type:
var c = JsonConvert.DeserializeObject<YourObjectTypeHere>(data.ToString());
//list or dictionary object type
var c1 = JsonConvert.DeserializeObject< ComplexObject1 >(data.c1.ToString());
var c2 = JsonConvert.DeserializeObject< ComplexObject2 >(data.c2.ToString());
...
}
// object type in serverside
public class WfAppRunner
{
public string AppName { get; set; }
public int AppInstanceID { get; set; }
public string AppInstanceCode { get; set; }
public Guid ProcessGUID { get; set; }
public string FlowStatus { get; set; }
public int UserID { get; set; }
public string UserName { get; set; }
public IDictionary<Guid, PerformerList> NextActivityPerformers { get; set; }
public IDictionary<string, string> Conditions { get; set; }
public string Other { get; set; }
}
public class Performer
{
public int UserID { get; set; }
public string UserName { get; set; }
}
public class PerformerList : List<Performer>
{
public PerformerList() {}
}

webapi post 请求多个参数的更多相关文章
- ASP.NET WebApi 学习与实践系列(2)---WebApi 路由请求的理解
目录 写在前面 WebApi Get 请求 1.无参数的请求 2.一个参数的请求 3.多个参数的请求 4.实体参数的请求 WebApi Post 请求 1.键值对请求 2.dynamic 动态类型请求 ...
- ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)
业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...
- Asp.Net WebApi Post请求整理(一)
Asp.Net WebApi+JQuery Ajax的Post请求整理 一.总结 1.WebApi 默认支持Post提交处理,返回的结果为json对象,前台不需要手动反序列化处理.2.WebApi 接 ...
- WebApi 异步请求(HttpClient)
还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 废话不多说,直接进入正题: 今天公司总部要求各个分公司把短信接口对接上,所谓的 ...
- Web API系列(四) 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录
转自:https://www.cnblogs.com/hnsongbiao/p/7039666.html 需要demo在github中下载: https://github.com/shan333cha ...
- (三)Asp.net web api中的坑-【http post请求中的参数】
接上篇, HttpPost 请求 1.post请求,单参数 前端 var url = 'api/EnterOrExit/GetData2';var para = {};para["Phone ...
- (二)Asp.net web api中的坑-【http get请求中的参数】
webapi主要的用途就是把[指定的参数]传进[api后台],api接收到参数,进行[相应的业务逻辑处理],[返回结果].所以怎么传参,或者通俗的说,http请求应该怎么请求api,api后台应该怎么 ...
- Web APi之捕获请求原始内容的实现方法以及接受POST请求多个参数多种解决方案(十四)
前言 我们知道在Web APi中捕获原始请求的内容是肯定是很容易的,但是这句话并不是完全正确,前面我们是不是讨论过,在Web APi中,如果对于字符串发出非Get请求我们则会出错,为何?因为Web A ...
- 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录
使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录 C#进阶系列——WebApi 异常处理解决方案 [ASP.NET Web API教程]4.3 AS ...
随机推荐
- WebView
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- Github上安卓榜排名第2的程序员教你如何学习【转载,侵删】
来自:峰瑞资本(微信号:freesvc)文章作者:代码家(微信 ID:daimajia_share) 软件早已吞噬整个世界,程序员是关键角色.过去 40 年中,许多伟大的公司都由程序员缔造,比如比尔· ...
- 分享Kali Linux 2016.2第50周镜像文件
分享Kali Linux 2016.2第50周镜像文件Kali Linux官方于12月11日发布Kali Linux 2016.2的第50周镜像.这次保持以往规律,仍然是11个镜像文件.默认的Gnom ...
- python操作mongodb数据库
一.MongoDB 数据库操作 连接数据库 import pymongo conn = pymongo.Connection() # 连接本机数据库 conn = pymongo.Connection ...
- Python2 连接MySQL
先安装MySQL-python yum install -y MySQL-python 测试代码: # -*- coding: utf-8 -*- import os import MySQLdb i ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- MySQL sql_safe_updates 分析
我在练习MySQL操作语句时,使用一条完全没有错误的语句: update students set name='drake' where name='chuan'; 却报了如下错误: Error Co ...
- Daily Scrum02 12.10
不同于往常,今天我们在中午体育课后,就简单的进行了一次小组会议,主要是讨论了一下,我们的单词软件的查询功能的优化的进度与暂时达到的效果,界面的各个按钮等元素的素材的准备有点拖慢,由于要反复修改,查看效 ...
- requirejs:研究笔记
模块化历史 模块化异步加载方式 后期维护 查找问题 复用代码 防止全局变量的污染 http://requirejs.cn/ http://requirejs.org/ 我的目录结构 总体步骤 < ...
- PDMS模型导出RVM格式
2 .将PDMS中对象模型导出为RVM格式的宏文件代码如下: eg:如果要导出某几个房间内的全部bran equi !strus = array()!strus.append(|/1RXR246ZL| ...