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":{
"39‌​c71004-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 请求多个参数的更多相关文章

  1. ASP.NET WebApi 学习与实践系列(2)---WebApi 路由请求的理解

    目录 写在前面 WebApi Get 请求 1.无参数的请求 2.一个参数的请求 3.多个参数的请求 4.实体参数的请求 WebApi Post 请求 1.键值对请求 2.dynamic 动态类型请求 ...

  2. ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)

    业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...

  3. Asp.Net WebApi Post请求整理(一)

    Asp.Net WebApi+JQuery Ajax的Post请求整理 一.总结 1.WebApi 默认支持Post提交处理,返回的结果为json对象,前台不需要手动反序列化处理.2.WebApi 接 ...

  4. WebApi 异步请求(HttpClient)

    还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 废话不多说,直接进入正题: 今天公司总部要求各个分公司把短信接口对接上,所谓的 ...

  5. Web API系列(四) 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录

    转自:https://www.cnblogs.com/hnsongbiao/p/7039666.html 需要demo在github中下载: https://github.com/shan333cha ...

  6. (三)Asp.net web api中的坑-【http post请求中的参数】

    接上篇, HttpPost 请求 1.post请求,单参数 前端 var url = 'api/EnterOrExit/GetData2';var para = {};para["Phone ...

  7. (二)Asp.net web api中的坑-【http get请求中的参数】

    webapi主要的用途就是把[指定的参数]传进[api后台],api接收到参数,进行[相应的业务逻辑处理],[返回结果].所以怎么传参,或者通俗的说,http请求应该怎么请求api,api后台应该怎么 ...

  8. Web APi之捕获请求原始内容的实现方法以及接受POST请求多个参数多种解决方案(十四)

    前言 我们知道在Web APi中捕获原始请求的内容是肯定是很容易的,但是这句话并不是完全正确,前面我们是不是讨论过,在Web APi中,如果对于字符串发出非Get请求我们则会出错,为何?因为Web A ...

  9. 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录

    使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录 C#进阶系列——WebApi 异常处理解决方案 [ASP.NET Web API教程]4.3 AS ...

随机推荐

  1. STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱?

    STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱? STM32F系列单片机芯片解密型号: STM32F100  |  STM32F101  |  STM32F102  | ...

  2. BurpSuite设置公共WIFI抓包

    1.电脑连接公共WIFI

  3. PHP 检查并创建多级目录

    <?php //检查并创建多级目录    function checkDir($path){        $pathArray = explode('/',$path);        $no ...

  4. Page-encoding specified in XML prolog (UTF-8) is different from that specified in page directive (utf-8)

    org.apache.jasper.JasperException:xxx.jsp(1,1) Page-encoding specified in XML prolog (UTF-8) is diff ...

  5. mysql解压版安装

    1.下载MySQL解压版(32位) http://dev.mysql.com/downloads/mysql/

  6. JDBC、JTA、Spring的事务管理

    Java事务的类型有三种:JDBC事务.JTA(Java Transaction API)事务.容器事务. 事务就是对一系列的数据库操作(比如插入多条数据)进行统一的提交或回滚操作,如果插入成功,那么 ...

  7. C# 禁止修改已装箱了的值类型的字段值,但是可以通过接口的方式实现

    C# 默认是不能修改已装箱了的值类型中字段的值,但是可以通过 值类型实现指定的接口来改变 首先定义一个接口 interface IChange { void Change(int a, int b); ...

  8. go语言学习笔记

    Go语言学习基本类型Bool 取值范围:true,false (不可以用数字代替)Int/uint 根据平台可能为32或64位int8/uint8 长度:1字节 取值范围-128~127/0~255b ...

  9. 实战Java虚拟机之四:提升性能,禁用System.gc() ?

    今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...

  10. mysql排序时索引的注意事项

    参考 'mysql高性能'