Tom*_*sen 9 c# model-binding asp.net-web-api
我需要将传入的HTTP POST请求与正文中的数据绑定到具体类型,具体取决于ProductType数据中的分母.这是我的Web API 2操作方法:
[HttpPost, Route]
public HttpResponseMessage New(ProductBase product)
{
// Access concrete product class...
if (product is ConcreteProduct)
// Do something
else if (product is OtherConcreteProduct)
// Do something else
}
Run Code Online (Sandbox Code Playgroud)
我首先考虑使用自定义模型绑定器,但似乎无法在此时访问请求主体:
对于复杂类型,Web API尝试使用媒体类型格式化程序从邮件正文中读取值.
我无法真正看到媒体类格式化器如何解决这个问题,但我可能会遗漏一些东西.你会如何解决这个问题?
Dar*_*rov 15
根据请求内容类型,您必须决定实例化哪个具体类.我们来举个例子吧application/json.对于这种开箱即用的内容类型,Web API使用JSON.NET框架将请求主体有效负载反序列化为具体对象.
因此,您必须使用此框架才能实现所需的功能.这个框架中一个很好的扩展点就是编写一个自定义JsonConverter.假设您有以下类:
public abstract class ProductBase
{
public string ProductType { get; set; }
}
public class ConcreteProduct1 : ProductBase
{
public string Foo { get; set; }
}
public class ConcreteProduct2 : ProductBase
{
public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并执行以下操作:
public HttpResponseMessage Post(ProductBase product)
{
return Request.CreateResponse(HttpStatusCode.OK, product);
}
Run Code Online (Sandbox Code Playgroud)
让我们编写一个自定义转换器来处理这种类型:
public class PolymorphicProductConverter: JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ProductBase);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
ProductBase product;
var pt = obj["productType"];
if (pt == null)
{
throw new ArgumentException("Missing productType", "productType");
}
string productType = pt.Value<string>();
if (productType == "concrete1")
{
product = new ConcreteProduct1();
}
else if (productType == "concrete2")
{
product = new ConcreteProduct2();
}
else
{
throw new NotSupportedException("Unknown product type: " + productType);
}
serializer.Populate(obj.CreateReader(), product);
return product;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
最后一步是在以下位置注册此自定义转换器WebApiConfig:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
new PolymorphicProductConverter()
);
Run Code Online (Sandbox Code Playgroud)
这就是它.现在您可以发送以下请求:
POST /api/products HTTP/1.1
Content-Type: application/json
Host: localhost:8816
Content-Length: 39
{"productType":"concrete2","bar":"baz"}
Run Code Online (Sandbox Code Playgroud)
并且服务器将正确反序列化此消息并响应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
Date: Sat, 25 Jan 2014 12:39:21 GMT
Content-Length: 39
{"Bar":"baz","ProductType":"concrete2"}
Run Code Online (Sandbox Code Playgroud)
如果您需要处理其他格式,例如application/xml您可能会执行相同的操作并插入相应的序列化程序.
| 归档时间: |
|
| 查看次数: |
2726 次 |
| 最近记录: |