最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Object posted to api is Null in .NET 8 client but works in Framework 4.8 - Stack Overflow

programmeradmin0浏览0评论

I have a client winform application originally targeting .NET framework 4.8 that I have upgraded to .NET 8. The client sends the class to a local server MVC.3 api that saves the entity to the server. The code works in the 4.8 framework application but in the .NET 8 application the object is received by the api as null. The code has not been changed and it compiles and runs without errors, except for the fact that the object is not passed. The Newtonsoft.Json package is 13.3, the same for both apps. Here is my client code:

public async Task<string> putBudgetasync(Institution_Budget ib)
{

    HttpClientHandler handler = new HttpClientHandler();
    handler.UseDefaultCredentials = true;
    using (var client = new HttpClient(handler))
    {
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
        var myuri = new Uri(urlbase + "api/InstBudget/");
        HttpResponseMessage response =
            await client.PutAsJsonAsync(myuri, ib); // I suspect the putasjson is not working in .NET 8 as it did in the framework app
        if (response.IsSuccessStatusCode)
        {
            return "OK_putBudget";
        }
        else
        {
            return "Error_putBudget";
        }
    }

Here is the api controller code. As stated when the call is made from the client ib is not null, it is received by the controller as null (in .NET 8 client but not Framework 4.8). The api is hit only the code fails in the first line as the object ib is null.

[ResponseType(typeof(void))]
[System.Web.Http.HttpPut]
[Route("api/InstBudget/")]
 public async Task<IHttpActionResult> PutInstitution_Budget([FromBody] Object 
     sourceinstitution_Budget)  // The source institution_Budget is null in .NET 8
{
  Institution_Budget_Sys GetterObject = Repo.ConverttoServerObject(sourceinstitution_Budget) 
  long counter = (long)GetterObject.GetType()
      .GetProperty("Counter")
      .GetValue(GetterObject, null);
  string instcode =
      (string)
          GetterObject.GetType().GetProperty("InstCode").GetValue(GetterObject, null);
      Institution_Budget_Sys institutionbudget = GetInstbyCounterInstcode(counter, 
     instcode);
  if (sourceinstitution_Budget == null)
  {
      return BadRequest();
  }

  Reposotories.Repository.DTOtransfer(institutionbudget, GetterObject);

  try
  {
      await db.SaveChangesAsync();
  }
  catch (DbUpdateConcurrencyException)
  {
      if (!Institution_BudgetExists(institutionbudget.id))
      {
          return NotFound();
      }
      else
      {
          throw;
      }
  }
  return StatusCode(HttpStatusCode.NoContent);
  }
发布评论

评论列表(0)

  1. 暂无评论