本部分纯属代码,如有疑问,请参考之前的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部分
四、输出