简体   繁体   中英

WebAPI OData v4 Actions

First off, I have this problem ...

WebApi OData v4 ComlexType cannot have an EntityType

... having figured "that's rubbish but maybe I can work around it" I thought I might try telling OData that I am just returning a string and Json serialise the object myself ...

Here's what i'd like to do

var acptBatch = Builder.EntityType<ProposedOffer>().Collection.Action("AcceptBatch");
acptBatch.Parameter<bool>("asSingleTransaction");
acptBatch.Parameter<string>("perspective");
acptBatch.ReturnsCollection<ValidationResult<ProposedOffer>>();

[HttpPost]
[ODataRoute("AcceptBatch({asSingleTransaction},{perspective})")]
public async Task<IHttpActionResult> AcceptBatch([FromUri]bool asSingleTransaction, [FromUri]string perspective, [FromBody]ICollection<int> offerIds)
{
    try
    {
        var p = (Perspective)Enum.Parse(typeof(Perspective), perspective);
        var result = await Service.AcceptOffers(asSingleTransaction, offerIds, p);

        return Ok(result);
    }
    catch (Exception ex)
    {
        return await Failed(ex);
    }
} 

having battled with it for an hour or so already i've gotten this far ...

var acptBatch = Builder.EntityType<ProposedOffer>().Collection.Action("AcceptBatch");
acptBatch.Parameter<bool>("asSingleTransaction");
acptBatch.Parameter<string>("perspective");

[HttpPost]
[ODataRoute("AcceptBatch({asSingleTransaction},{perspective})")]
[Route("ProposedOffer/AcceptBatch({asSingleTransaction},{perspective})")]
public async Task<IHttpActionResult> AcceptBatch([FromUri]bool asSingleTransaction, [FromUri]string perspective)
{
    try
    {
        var offerIds = JsonConvert.DeserializeObject<ICollection<int>>(await Request.Content.ReadAsStringAsync());
        var p = (Perspective)Enum.Parse(typeof(Perspective), perspective);
        var result = await Service.AcceptOffers(asSingleTransaction, offerIds, p);

        return Ok();
    }
    catch (Exception ex)
    {
        return await Failed(ex);
    }
}

Anyone got any ideas how I can get my result back to the calling javascript based code on the client? At this point putting anything inside that Ok() results in the framework returning a 406 no matter how I declare it when initialising the model from what I can tell.

After much confusion on my part and much agruing with the compiler / OData framework about what's right and wrong and the meaning of life I came ot the conclusion that this wass just simpler to do out of OData and in a simple WebAPI method.

It seems that the OData framework today is too immature and lacking the basic ability to perform some of the more complex tasks (particularly those that are not related to querying) which is really odd because it's sat on top of a framework that can handle this perfectly fine!

Advice for others facing this problem ... If OData fails you, create a simple WebAPI controller and it just simply won't fail you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM