博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【WEBAPI】常用参数传递方法总结
阅读量:5808 次
发布时间:2019-06-18

本文共 5085 字,大约阅读时间需要 16 分钟。

本部分纯属代码,如有疑问,请参考之前的BLOG文章

一、C#部分

1.1 实体类

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Runtime.Serialization;using Newtonsoft.Json;namespace TestOauth.Models{    //[Serializable]    [DataContract]    //[JsonObject(MemberSerialization.OptIn)]    public class OAuthModels    {        public OAuthModels()        {        }        //[DataMember]        [JsonIgnore]        public string ID        {            get;            set;        }        //[JsonProperty(PropertyName = "oauth_consumer_key", NullValueHandling = NullValueHandling.Ignore)]        [DataMember(Name = "oauth_consumer_key")]        public string Consumer_key        {            get;            set;        }        [JsonIgnore]        public string Consumer_secret        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_signature_method", NullValueHandling = NullValueHandling.Ignore)]        public string Signature_method        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_timestamp", NullValueHandling = NullValueHandling.Ignore)]        public string Timestamp        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_nonce", NullValueHandling = NullValueHandling.Ignore)]        public string Nonce        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_signature", NullValueHandling = NullValueHandling.Ignore)]        public string Signature        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_token", NullValueHandling = NullValueHandling.Ignore)]        public string Token        {            get;            set;        }        [JsonProperty(PropertyName = "oauth_token_secret", NullValueHandling = NullValueHandling.Ignore)]        public string Token_secret        {            get;            set;        }    }}

 

1.2 ACTION方法

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using TestOauth.Models;using Newtonsoft.Json.Linq;namespace TestOauth.Controllers{    public class WebAPITestController : ApiController    {        [HttpGet]        public string TestGet()        {            return "HelloWorld";        }        [HttpGet]        public OAuthModels TestGetString(string model)        {            OAuthModels d = Newtonsoft.Json.JsonConvert.DeserializeObject
(model); d.Token = "requestkey"; d.Token_secret = "requestsecret"; return d; } [HttpPost] public OAuthModels TestPOSTString([FromBody]string model) { OAuthModels d = Newtonsoft.Json.JsonConvert.DeserializeObject
(model); d.Token = "requestkey"; d.Token_secret = "requestsecret"; return d; } [HttpPost] public OAuthModels TestPOSTModel([FromBody]OAuthModels model) { model.Token = "requestkey"; model.Token_secret = "requestsecret"; return model; } ///
/// didn't support this method /// ///
///
///
[HttpPost] public OAuthModels TestMultipleWithWrongMethod([FromBody]OAuthModels model, string userToken) { model.Token = "requestkey"; model.Token_secret = "requestsecret"; return model; } [HttpPost] public OAuthModels TestMultipleWithJObject(Newtonsoft.Json.Linq.JObject jobj) { dynamic d = jobj; JObject oauthModel = d.model; JObject oauthModel2 = d.model2; string token = d.userToken; var model = oauthModel2.ToObject
(); model.Token = "requestkey"; model.Token_secret = "requestsecret"; return model; } [HttpPost] public OAuthModels MyAction(HttpRequestMessage request) { // make explicit calls to get parameters from the request object int id = int.Parse(request.RequestUri.ParseQueryString().Get("id")); // need error logic! OAuthModels model = request.Content.ReadAsAsync
().Result; // should be async! // Now use id and customer model.Token = "requestkey"; model.Token_secret = "requestsecret"; return model; } }}

 

 

二、HTML部分

    TestCase                

Passing single Parameters to a Web API Controller

测试 单一参数POST STRING 类型

测试 单一参数GET STRING 类型

测试 Test POST Model

Passing multiple Parameters to a Web API Controller

测试 多个参数 类型

 

 

三、JS部分

 

四、输出

 

 

 

转载于:https://www.cnblogs.com/taoqianbao/archive/2013/02/25/2931809.html

你可能感兴趣的文章
Apache配置
查看>>
Ext gridPanel 单元格数据的渲染
查看>>
Android SDK 的下载代理
查看>>
Method Swizzling对Method的要求
查看>>
佛祖保佑,永不宕机
查看>>
四、配置开机自动启动Nginx + PHP【LNMP安装 】
查看>>
LNMP一键安装
查看>>
SQL Server数据库概述
查看>>
Linux 目录结构及内容详解
查看>>
startx命令--Linux命令应用大词典729个命令解读
查看>>
华为3026c交换机配置tftp备份命令
查看>>
Oracle命令导入dmp文件
查看>>
OCP读书笔记(24) - 题库(ExamD)
查看>>
Http、TCP/IP协议与Socket之间的区别(转载)
查看>>
解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790
查看>>
.net excel利用NPOI导入oracle
查看>>
vrpie在Visio Studio 中无法调试的问题
查看>>
第六课:数据库的基本工具
查看>>
关于二叉树重构的思索
查看>>
$_SERVER['SCRIPT_FLENAME']与__FILE__
查看>>