webapi fromurl frombody

https://blog.csdn.net/QiGary/article/details/113979877

 

在做后台api接口时,常常涉及到Http方法访问问题,其中最基础也是最核心的就是传参问题。在基于C#的webapi项目中,其传参有两种实现方式,一种是使用[FromBody]和[FromUri]作为Http接口函数形参前缀传参,另一种是将函数的形参设置为空,使用System.Web.Http命名空间下的【HttpContext.Current.Request】获取。

第一种,使用[FromBody]和[FromUri]作为Http接口函数形参前缀传参

eg1:在url中传参访问,即后端使用[FromUri]

[Route("ParamsGetTest"), HttpGet]
public string ParamsGetTest([FromUri]string param1,[FromUri]string param2)
{
string result = "none";
return result;
}

[Route("ParamsPostTest"), HttpPost]
public string ParamsPostTest([FromUri]string param1, [FromUri]string param2)
{
string result = "none";
return result;
}

[Route("ParamsPostTest"), HttpPost]
public string ParamsPostTest()
{
string param1= HttpContext.Current.Request.Params["param1"].Trim();
string param2= HttpContext.Current.Request.Params["param2"].Trim();
return "ok";
}
此种接口允许url传参,其中,参数名【param1】和【param2】要与接口一致。访问形式如:https://localhost:44358/Test/ParamsPostTest?param1=admin&param2=admin

在postman测试时,直接在【Params】标签页传参即可。

 

eg2:在Body中传参访问,即后端使用[FromBody]

//JObject 传值(推荐使用,可以转化为任意实体对象)
[Route("ParamsPostTest"), HttpPost]
public string ParamsPostTest([FromBody]JObject obj)
{
//obj任意名称
RequestBody result = null;
if (obj != null)
{
result = obj.ToObject<RequestBody>();
}
return JsonConvert.SerializeObject(result);
}

//具体实际对象直接传值 T=RequestBody
[Route("ParamsPostTest"), HttpPost]
public string ParamsPostTest([FromBody]RequestBody obj)
{
//obj任意名称
return JsonConvert.SerializeObject(obj);
}

//字符串传值 可为json串
[Route("ParamsPostTest"), HttpPost]
public string ParamsPostTest([FromBody]string obj)
{
//obj任意名称
return JsonConvert.SerializeObject(obj);
}

get方法使用与Post一致,其中,参数名【obj】任意。

在postman测试时,在【Body】标签页【x-www-form-urlencoded】和【raw】选项内,均可实现访问测试,推荐使用【raw】选项测试。

 


————————————————
版权声明:本文为CSDN博主「QiGary」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/QiGary/article/details/113979877

 

在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流。

举例子说明:

1. 请求地址:/?id=123&name=bob 

    服务端方法: void Action(int id, string name) // 所有参数都是简单类型,因而都将来自url

2. 请求地址:/?id=123&name=bob 

    服务端方法: void Action([FromUri] int id, [FromUri] string name) // 同上

                      void Action([FromBody] string name); //[FormBody]特性显示标明读取整个body为一个字符串作为参数

3. 请求地址: /?id=123 

    类定义:

public class Customer {   // 定义的一个复杂对象类型 
  public string Name { get; set; } 
  public int Age { get; set; } 
}

    服务端方法: void Action(int id, Customer c) // 参数id从query string中读取,参数c是一个复杂Customer对象类戏,通过formatter从body中读取

    服务端方法: void Action(Customer c1, Customer c2) // 出错!多个参数都是复杂类型,都试图从body中读取,而body只能被读取一次

    服务端方法: void Action([FromUri] Customer c1, Customer c2) // 可以!不同于上面的action,复杂类型c1将从url中读取,c2将从body中读取

4.ModelBinder方式:

void Action([ModelBinder(MyCustomBinder)] SomeType c) // 标示使用特定的model binder来解析参数

[ModelBinder(MyCustomBinder)] public class SomeType { } // 通过给特定类型SomeType声明标注[ModelBidner(MyCustomBinder)]特性使得所有SomeType类型参数应用此规则 

void Action(SomeType c) // 由于c的类型为SomeType,因而应用SomeType上的特性决定其采用model binding

 

总结:

1. 默认简单参数都通过URL参数方式传递,例外:

1.1 如果路由中包含了Id参数,则id参数通过路由方式传递;

1.2 如果参数被标记为[FromBody],则可以该参数可以为简单参数,客户端通过POST方式传递:$.ajax(url, '=value'),或者$.ajax({url: url, data: {'': 'value'}});

2. 默认复杂参数(自定义实体类)都通过POST方式传递,例外:

2.1 如果参数值被标记为[FromUri], 则该参数可以为复杂参数;

3. 被标记为[FromBody]的参数只允许出现一次, 被标记为[FromUri]的参数可以出现多次,如果被标记为[FromUri]的参数是简单参数,该标记可以去掉。

 

 
 
 
posted on 2014-08-11 09:12 Juvy 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Juvy/p/3903974.html

 

posted on 2022-07-28 08:45  rightway  阅读(65)  评论(0编辑  收藏  举报

导航