ITKeyword,专注技术干货聚合推荐

注册 | 登录

asp.net - MediaFormatter or ModelBinder for web api PUT method

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:ASP.NET Web API

ASP.NET Web API includes support for the following features: Modern HTTP programming model: Directly access and manipulate HTTP requests and responses

up vote 0 down vote favorite I have a PUT method in web api which accepts a JSON data and a route data as follows. [Route("api/v1/Orders/{orderId}/active")]

public HttpResponseMessage Put(Guid? orderId,List<ActiveRequest> activeRequests)

{

}

public class ActiveRequest

{

public int Id { get; set; }

public bool IsActive { get; set; }

}

Now is it possible to simplify the method signature as: [Route("api/v1/Orders/{orderId}/active")]

public HttpResponseMessage Put(ActiveRequestModel model)

{

}

public class ActiveRequestModel

{

public Guid OrderId { get; set; }

public List<ActiveRequest> ActiveRequests {get; set;}

}

I tried writing a custom ModelBinder by implementing the System.Web.Http.ModelBinding.IModelBinder interface but could'nt find a way to read the JSON data that is coming inside the Request object. I doubt that is there a way by which I can bind my model with data coming from three different places i.e. from route data, json & form. asp.net asp.net-mvc asp.net-web-api asp.net-web-api2 model-binding
  |
  this question edited May 4 '15 at 14:10 asked May 3 '15 at 6:50 thinkmmk 222 1 5 18



 |  1 Answers

up vote 1 down vote ---Accepted---Accepted---Accepted---

You cannot simplify the parameter as described. Unlike MVC model binding, beacuse of how the Web API formatter works, in Web API you only can have a single parameter that is deserialized from the payload, and a number of simple type parameters coming from route parameters or url query string. The reason is that the creation of the parameter coming from the payload is done in a single pass deserialization of the payload. So, for your example you need the two parameters in your original version, i.e.: public HttpResponseMessage Put(Guid? orderId, List<ActiveRequest> activeRequests)

If you want to use the ActiveRequestModel you need to include a payload which has exactly the same structure, so you should include the orderId in the payload, because it will not be recovered from the url (even if the name matches). Please, read this article which explains how parameter binding works in Web API: Parameter Binding in ASP.NET Web API If you read it thoroughly you'll see that you can create and register your own model binder to make it work the same way that an MVC controller, but I think it's not worth the effort (so I inclu

推荐:ASP.NET Web API :Web宿主

上一节描述了API的整个运行框架,即分为三层hosting、message handler pipeline 和 controller handling。此节讲其中一个宿主,WebHost 寄宿在asp.net 传统管道

de it only in this last paragraph), and it's not the standard way of working.
  |
  this answer edited May 4 '15 at 20:04 answered May 4 '15 at 13:53 JotaBe 24.2k 5 44 75      Thanks @JotaBe, but my OrderId won't be a part of the JSON payload. It will be coming from the route. like my url will have that data exp: localhost:12345/api/v1/Orders/… –  thinkmmk May 4 '15 at 14:11 1   @thinkmmk I'm sorry I didn'd understand you question. I've fully edited my answer now. You cannot do what you want, unless you include the order id in the payload, or define an use your own model binder –  JotaBe May 4 '15 at 20:04      thanks again for clarifying the same, so a custom model binder can fulfill my requirements or do i have to include the orderid inside json? –  thinkmmk May 6 '15 at 16:52      It's up to you: if you think that you can easyly implement a model binder which solves most of your bindings, then do it. If you think that you'll have to create lots of different model binders, or the implementation it's difficult, take the easy path, and include it in the JSON, which is pretty easy. I'm not discouraging to do it, but warning you that if it gets too complicated it's better to do it the "usual way". –  JotaBe May 6 '15 at 17:19 1   There are several properties to get the raw JSON data from the request, for example Request.Content.ReadAsStringAsync(). Then you can use JSON.NET to deserialize it. Implementing the Model Binder is complex because you only have access to the raw data, and the headers to determine which kind of data is being posted. Your model binder has to determine if it's JSON to deserialize as JSON, or is form data, to read it as form data, or is any other format to read it. Quite complex. That's why I commented that probably it's not worth the effort. –  JotaBe May 7 '15 at 22:37  |  show 1 more comment

推荐:asp.net web api

官方的地址: http://www.asp.net/web-api 里面的资料很全,是学习的第一选择.建议多参考此资料 1.里面最简单直接的例子 http://www.asp.net/web-api/overview/get

up vote 0 down vote favorite I have a PUT method in web api which accepts a JSON data and a route data as follows. [Route("api/v1/Orders/{orderId}/active")]

public


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。