簡體   English   中英

通過Web請求進行PutAsync時缺少BsonDocument的值?

[英]Values of BsonDocument missing when PutAsync over web request?

我正在嘗試通過PutAsync請求將簡單的BsonDocument發送到WebAPI。

但是,將BsonDocument的值傳遞給WebAPI控制器時丟失

這是我客戶的代碼。

public static async Task<HttpResponseMessage> sendBdoc()
{
    try
    {
       var abc = new BsonDocument("Abc","hoho");

       var bformatter = new BsonMediaTypeFormatter();

       var id = ObjectId.GenerateNewId();

       HttpClient client = new HttpClient()
       client.BaseAddress = new Uri("http://localhost:58836");

       client.DefaultRequestHeaders.Accept.Clear();
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));

       var response = await client.PutAsync($"api/query/{id}", abc, bformatter);

       return response;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

這是我的控制器的代碼:

public async Task<IHttpActionResult> Put(string id, BsonDocument doc)
{
     try
     {
            //MongoDB Connection stuff here

            var filter = new BsonDocument("_id", ObjectId.Parse(id));

            var update = collection.ReplaceOneAsync(filter, doc, new UpdateOptions() { IsUpsert = true });
            var result = await update;

            if (result.IsAcknowledged)
            {
                return Ok();
            }

            return NotFound();
     }
     catch (Exception e)
     {
         return InternalServerError();
     }
}

調試客戶端應用程序的屏幕截圖,變量abc充滿了值。

https://i.stack.imgur.com/txp8l.png

Web控制器端的調試屏幕截圖,BsonDocument的值現在變為null。

https://i.stack.imgur.com/lBTe3.png

我會忽略什么嗎? 非常感謝

這可能是默認模型聯編程序的限制。 您能否嘗試從Put方法中刪除“字符串ID”並重新發送請求?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM