代码之家  ›  专栏  ›  技术社区  ›  Prisoner ZERO

在发布未绑定的复杂对象时,ODataActionParameters始终为空

  •  0
  • Prisoner ZERO  · 技术社区  · 6 年前

    我有一份工作 UNBOUND OData Complex Object ,命名为ShakeoutDocument。我正在尝试将相邻的“SaveShakeoutDocument”作为 未绑定

    有效载荷应如下所示: { document: { ...the Json of the Document... }}

    问题
    问题是…在执行各种组合时,我要么得到NULL ODataActionParameters

    • 我尝试过使用HttpClient…但没有成功

    EDM模型:“SaveShakeoutDocument”
    非常标准。。。

    var saveShakeoutDocument = modelBuilder.Action("SaveShakeoutDocument").ReturnsFromEntitySet<ShakeoutDocument>("ShakeoutDocument");
    saveShakeoutDocument.Parameter<ShakeoutDocument>("document");
    

    ODATA操作:“SaveShakeoutDocument” :
    非常标准。。。

    [HttpPost]
    [ODataRoute("SaveShakeoutDocument")]
    public IHttpActionResult SaveDocument(ODataActionParameters parameters)
    {
        // Doing awesome stuff here...
    }
    

    使用HTTP-CLIENT:“SaveShakeoutDocument”
    “document”变量被传入。。。

    using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(username, password) })
    using (var client = new HttpClient(handler))
    {
        var action = "SaveShakeoutDocument";
        var URL = string.Format("{0}{1}", BASE_URL, action);
    
        try
        {
            // Form Data - THIS FAILS: ODataActionParameters = NULL
            var jsonObject = JsonConvert.SerializeObject(document);
            var data = "{document: " + jsonObject + "}";
            var payload = new StringContent(data, Encoding.UTF8, "application/json");
    
            payload.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
            var httpResponse = client.PostAsync(URL, payload).Result;
            var json = await httpResponse.Content.ReadAsStringAsync();
    
        }
        catch (Exception ex)
        {
            Console.WriteLine("FAILURE: SaveShakeoutDocumentAsync (async)");
            throw;
        }
    }
    

    :

    • 内容类型:application/json

    原始的身体看起来像。。。

    {
        "@odata.context": "http://localhost:50806/$metadata#ShakeoutDocument/$entity",
        "DocumentTypeId": 1,
        "GlobalId": "e8c9d71d-2773-e911-b71a-8cdcd4471a95",
        "ParentId": null,
        "AuthorId": 1,
        "PublisherId": null,
        "RevisionNumber": 0,
        "PublishedDate": null,
        "IsActive": true,
        "Id": 44,
        "CreateUserId": "domain\\lanid",
        "CreateDate": "2019-05-10T08:25:46.31-05:00",
        "UpdateUserId": "domain\\lanid",
        "UpdateDate": "2019-05-10T08:25:46.31-05:00",
        "ShakeoutId": 44,
        "SchedulingBatch": null,
        "ProductId": null,
        "Gravity": null,
        "Temperature": null,
        "SedimentAndWater": null,
        "BatchEndDate": null,
        "SampleWorkedDate": null,
        "Witness": null,
        "Notes": null,
        "Seals": [],
        "Details": [],
        "ObjectState": {
            "@odata.type": "#StateManagement.ShakeoutDocument.New",
            "Name": "New",
            "Events": [
                {
                    "@odata.type": "#StateManagement.ShakeoutDocument.IsNew",
                    "Name": "IsNew"
                }
            ]
        }
    }
    
    0 回复  |  直到 6 年前
    推荐文章