OData WebAPI Action的参数反序列化

时间:2021-02-04 15:13:55

try to use OData webAPI and call an action with a param, serialized into json without metainformation. So, I want to pass an object of a type:

尝试使用OData webAPI并使用param调用一个动作,序列化为json而不包含元信息。所以,我想传递一个类型的对象:

public class SomeRequest
{
    public RequestReason Reason { get; set; }
}

public enum RequestReason
{
    New,
    Dublicate
}

I've createed a mdel, configured an action:

我已经创建了一个mdel,配置了一个动作:

var action = modelBuilder.Entity<Member>().Action("SomeRequest");
action.Parameter<SomeRequest>("Info");
action.Returns<HttpResponseMessage>();
var model = modelBuilder.GetEdmModel();
configuration.EnableOData(model);

Have code in controller:

在控制器中有代码:

[HttpPost]
public HttpResponseMessage RequestIDCard(int key, [FromBody]ODataActionParameters param)
{
    object value;
    param.TryGetValue("Info", out value);
///!!!!
}

and expect to have value with real type SomeRequest, cast the type and process it... Then I make a POST request with headers

并期望具有真实类型SomeRequest的值,转换类型并处理它...然后我用标题发出POST请求

Content-Type: application/json;json=light; charset=utf-8 Accept: application/json;odata=light

Content-Type:application / json; json = light; charset = utf-8接受:application / json; odata = light

and body

{"Info":{"Reason":1}}

But I get object of type "Newtonsoft.Json.Linq.JObject" and sure it cannot be casted! But if I change object type to int, everything work :) Is it a bug of WebAPI OData or I do something wrong?

但我得到“Newtonsoft.Json.Linq.JObject”类型的对象,并确定它不能被铸造!但是,如果我将对象类型更改为int,一切正常:)这是WebAPI OData的错误还是我做错了什么?

1 个解决方案

#1


0  

Couple of things wrong with your usage,

你的用法有些不对劲,

  1. Enums are mapped to string's in aspnet Web API OData. So, your request body should have { "Reason" : 'Duplicate' instead.
  2. 枚举在aspnet Web API OData中映射到字符串。因此,您的请求正文应该有{“Reason”:'Duplicate'。

  3. As Jen has already ppointed out, application/json;odata=light is not a supported media type. You might want to use 'application/json;odata=minimalmetadata' or just 'application/json'.
  4. 正如Jen已经指出的那样,application / json; odata = light不是支持的媒体类型。您可能想要使用'application / json; odata = minimalmetadata'或只是'application / json'。

  5. action.Returns< HttpResponseMessage > is not useful. This would map HttpResponseMessage as a complex type in the EDM model of your service. I am not sure what the mapping would look like. Generally, you want to expose types that from your models in the EDM model that you are building. You should choose a more specific type from your models, more like,

    action.Returns 没用。这会将HttpResponseMessage映射为服务的EDM模型中的复杂类型。我不确定映射会是什么样子。通常,您希望在正在构建的EDM模型中公开模型中的类型。您应该从模型中选择更具体的类型,更像是,

    action.Returns< IDCard >();

    action.Returns ();

#1


0  

Couple of things wrong with your usage,

你的用法有些不对劲,

  1. Enums are mapped to string's in aspnet Web API OData. So, your request body should have { "Reason" : 'Duplicate' instead.
  2. 枚举在aspnet Web API OData中映射到字符串。因此,您的请求正文应该有{“Reason”:'Duplicate'。

  3. As Jen has already ppointed out, application/json;odata=light is not a supported media type. You might want to use 'application/json;odata=minimalmetadata' or just 'application/json'.
  4. 正如Jen已经指出的那样,application / json; odata = light不是支持的媒体类型。您可能想要使用'application / json; odata = minimalmetadata'或只是'application / json'。

  5. action.Returns< HttpResponseMessage > is not useful. This would map HttpResponseMessage as a complex type in the EDM model of your service. I am not sure what the mapping would look like. Generally, you want to expose types that from your models in the EDM model that you are building. You should choose a more specific type from your models, more like,

    action.Returns 没用。这会将HttpResponseMessage映射为服务的EDM模型中的复杂类型。我不确定映射会是什么样子。通常,您希望在正在构建的EDM模型中公开模型中的类型。您应该从模型中选择更具体的类型,更像是,

    action.Returns< IDCard >();

    action.Returns ();