3
votes

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:

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

public enum RequestReason
{
    New,
    Dublicate
}

I've createed a mdel, configured an action:

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

Content-Type: application/json;json=light; charset=utf-8 Accept: 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?

1
Just a quick note: the "odata=light" portion of your content-type and accept headers is from a pre-released version of the new JSON specification and has since been removed. Instead, use just "application/json" or "application/json;odata=minimalmetadata"Jen S
The same result. I use latest version of all libs from Nuget, System.Web.Http.OData v0.3.0.0Sergey

1 Answers

0
votes

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. 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'.
  3. 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< IDCard >();