locked
Consume JsonPatch HttpPatch Api in asp.net core RRS feed

  • Question

  • User2041008840 posted

    I have created the asp.net core webapi project and create HttpAtch api endpoint

      [HttpPatch("{id}")]
            public async Task<ActionResult<Setup>> PatchSetup(int id, JsonPatchDocument<Setup> jsonPatch)
            {
                var setup = await _context.Setup.FindAsync(id);
                if (setup == null)
                {
                    return NotFound();
                }
                jsonPatch.ApplyTo(setup);
                _context.Update(setup);
                await _context.SaveChangesAsync();
                return NoContent();
            }

    Model : 

    public class Setup

    {

    public string name {get;set;}

    public DateTime date {get;set;}

    }

    Now I want to use this api in asp.net core mvc project.
    How to I consume it?

    Thursday, April 8, 2021 4:24 PM

All replies

  • User-939850651 posted

    Hi Prathamesh Shende,

    Are you asking how to make a PATCH request from a C# application?

    If this is the case, you have to create an HttpRequestMessage manually and send it via SendAsync:

    var request = new HttpRequestMessage
    {
        RequestUri = new Uri("http://foo.com/api/foo"),
        Method = new HttpMethod("patch"),
        Content = new StringContent(json, Encoding.UTF8, "application/json-patch+json")
    };
    var response = await _client.SendAsync(request);

    You could also refer to this similar case: How to invoke the API action of type [HttpPatch] from HttpClient class in asp.net core mvc

    Best regards,

    Xudong Peng

    Friday, April 9, 2021 5:25 AM