WebApi接口使用HttpGet方式实现接受实体类参数(原创)
WebApi接口使用HttpGet方式实现接受实体类参数(原创)
C# Code:
/// <summary>
/// WebApi控制器接口使用HttpGet方法接受实体类参数
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet]
[Route("get-by-model")]
public ModelResponse TestGetByModel(
[FromUri(BinderType = typeof(ModelBinderFromUri<ModelRequest>))]
ModelRequest request)
{
string data = request.Data;
string s = request.ActionID.ToString();
return new ModelResponse { Code = 0, Message = "操作完成.", Data = "返回请求的数据:"+ data };
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// WebApi控制器接口使用HttpGet方法接受实体类参数
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet]
[Route("get-by-model")]
public ModelResponse TestGetByModel(
[FromUri(BinderType = typeof(ModelBinderFromUri<ModelRequest>))]
ModelRequest request)
{
string data = request.Data;
string s = request.ActionID.ToString();
return new ModelResponse { Code = 0, Message = "操作完成.", Data = "返回请求的数据:"+ data };
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
C# Code:
/// <summary>
/// 自定义ModelBinder处理程序
/// </summary>
/// <typeparam name="T"></typeparam>
public class ModelBinderFromUri<T> : System.Web.Http.ModelBinding.IModelBinder
{
public bool BindModel(HttpActionContext actionContext, System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
{
string JSON = "";
//使用GET方法是通过URL传递请求的数据(?参数)
if (actionContext.Request.Method.Method == "GET")
{
string uri = System.Web.HttpUtility.UrlDecode(actionContext.Request.RequestUri.Query.ToString());
if (!uri.IsNullOrEmpty() && uri.Length > 1)
{
if (uri.Substring(0, 1) == "?") //?{JSON字符串}
JSON = uri.Substring(1, uri.Length - 1);//去掉查询参数的?号
else
JSON = uri;
}
}
//使用POST方法是通过数据流传递数据
if (actionContext.Request.Method.Method == "POST")
{
using (StreamReader sr = new StreamReader(actionContext.Request.Content.ReadAsStreamAsync().Result))
{
JSON = sr.ReadToEnd();
}
}
try
{
//尝试将JSON转换为对象
T model = JsonConvert.DeserializeObject<T>(JSON);
//返回对象
bindingContext.Model = model;
}
catch (JsonReaderException)//JSON格式错误,抛出异常
{
throw new ResponseException(ErrorCodes.JsonFormatInvalide, ErrorCodes.JsonFormatInvalide_Msg);
}
return bindingContext.Model != null;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 自定义ModelBinder处理程序
/// </summary>
/// <typeparam name="T"></typeparam>
public class ModelBinderFromUri<T> : System.Web.Http.ModelBinding.IModelBinder
{
public bool BindModel(HttpActionContext actionContext, System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
{
string JSON = "";
//使用GET方法是通过URL传递请求的数据(?参数)
if (actionContext.Request.Method.Method == "GET")
{
string uri = System.Web.HttpUtility.UrlDecode(actionContext.Request.RequestUri.Query.ToString());
if (!uri.IsNullOrEmpty() && uri.Length > 1)
{
if (uri.Substring(0, 1) == "?") //?{JSON字符串}
JSON = uri.Substring(1, uri.Length - 1);//去掉查询参数的?号
else
JSON = uri;
}
}
//使用POST方法是通过数据流传递数据
if (actionContext.Request.Method.Method == "POST")
{
using (StreamReader sr = new StreamReader(actionContext.Request.Content.ReadAsStreamAsync().Result))
{
JSON = sr.ReadToEnd();
}
}
try
{
//尝试将JSON转换为对象
T model = JsonConvert.DeserializeObject<T>(JSON);
//返回对象
bindingContext.Model = model;
}
catch (JsonReaderException)//JSON格式错误,抛出异常
{
throw new ResponseException(ErrorCodes.JsonFormatInvalide, ErrorCodes.JsonFormatInvalide_Msg);
}
return bindingContext.Model != null;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
C# Code:
string url = "http://localhost:8899/api/testapi/get-by-model";
ModelRequest M = new ModelRequest();
M.ActionID = 100000;
M.ApiKey = "5cd31880a3f548ee9a8f37768bd6ebc8";
M.Data = "使用HttpGet方法接受实体类参数演示";
M.Sign = "s34df15cd31880a3f548ee9a8f37768bd6ebc8";
string JSON = JsonConvert.SerializeObject(M);
string result = WebApiTools.Get(url, JSON);
txtResponse.Text = result;
//来源:C/S框架网(www.csframework.com) QQ:1980854898
string url = "http://localhost:8899/api/testapi/get-by-model";
ModelRequest M = new ModelRequest();
M.ActionID = 100000;
M.ApiKey = "5cd31880a3f548ee9a8f37768bd6ebc8";
M.Data = "使用HttpGet方法接受实体类参数演示";
M.Sign = "s34df15cd31880a3f548ee9a8f37768bd6ebc8";
string JSON = JsonConvert.SerializeObject(M);
string result = WebApiTools.Get(url, JSON);
txtResponse.Text = result;
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网