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 ...
随机推荐
- SQL Server 2012 新增语法
--连接两个字符串. CONCAT(TelePhone,UserName,' : ',LoginVCode) FROM [dbo].[TB_NUsers] --SQL Server2012新增了两个逻 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- node 关键点总结
1.I/O密集的地方尽量不要用require.(require是同步I/O操作) eg:正在运行一个HTTP服务器,如果在每个进入的请求上都用了require,就会遇到性能问题.所以通常在程序最初加载 ...
- gulp自动化构建
最近正在使用gulp去帮我自动化构建一些技术块,感觉很爽,所以把gulp操作步骤给写笔记,记录下来... 首先了解什么是gulp? 我的理解是一个工具并且自动化的,能帮你把一些前端技术的语法转换成当前 ...
- 使用tungsten将mysql的数据同步到hadoop
背景 线上有很多的数据库在运行,后台需要一个分析用户行为的数据仓库.目前比较流行的是mysql和hadoop平台. 现在的问题是,如何将线上的mysql数据实时的同步到hadoop中,以供分析.这篇文 ...
- Ubuntu install Docker
首先需要说明的是,根据Docker的官方文档,Docker的安装必须在64位的机子上.这里只说明Ubuntu 14.04与16.04,我成功安装成功过Ubuntu 14.04,16.04还没有测试过, ...
- Java图片转换为base64格式
/** * @Descriptionmap 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param pa ...
- 【emWin】例程五:显示数值
实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1pLexsAf密码:p0jf 实验现象:
- java并发容器类
本文主要介绍java并发容器相关实现类,collections节点下接口方法介绍. Queue Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是Blocking ...
- 常用数据库的驱动程序及JDBC URL:
Oracle数据库: 驱动程序包名:ojdbc14.jar 驱动类的名字:oracle.jdbc.driver.OracleDriver JDBC URL:jdbc:oracle:thin:@dbip ...