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 ...
随机推荐
- Vue#组件
组件: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能. 使用: ...
- MyBatis - MyBatis Generator 生成的example 如何使用 and or 简单混合查询
简单介绍: Criteria,包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系. oredCriteria,Example内有一个 ...
- SpringMvc的创建流程以及2种加载配置文件的方式
1.首先创建个web项目,第一步导入相应的jar包,并且buildtoPath 2.用elipse或myeclipse点击进入web.xml中 按住 Alt+ / 有个提示 找到前面带 #Dispat ...
- 简单Excel表格上传下载,POI
一.废话 Excel表格是office软件中的一员,几乎是使用次数最多的办公软件.所以在java进行企业级应用开发的时候经常会用到对应的上传下载便利办公. 目前,比较常用的实现Java导入.导出Exc ...
- Android之菜单总结
在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 1. 选项菜单(OptionsMenu)详解 Activi ...
- 为什么<b></b>不推荐使用
曾经在网上看见说:不推荐是用b标签,咦,我好像用过不少,难道我又坑了别人……度娘是这样说的:只要是从网页的简洁性和搜索引擎的友好度来看的.<b>是加粗,和css的font-weight在视 ...
- Glyphicon 字体图标
Bootstrap中的Glyphicon 字体图标 在Bootstrap框架中也为大家提供了近200个不同的icon图片,而这些图标都是使用CSS3的@font-face属性配合字体来实现的icon效 ...
- win10 install JDK&&JRE
重装系统后,安装的java环境没了,只能重装一下~~~~ 1.下载JDK 2.这里会安装两次,其中第一次为安装 JDK,第二次安装JRE,建议不要将这两个放在同一个文件夹. 3.配置环境变量 用鼠标右 ...
- 天猫登录源码 POST C#
HttpHelper 请从网络中搜索: public partial class LoginTMall : Form { public LoginTMall() { InitializeCompone ...
- JQuery 实现锚点链接之间的平滑滚动
24. 解决链接锚点的生硬问题 $('.nav .btn[href*=#],.icon2,.icon3').click(function() { if (location.pathname.repla ...