Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp web api patch implementation

Assume i have this model

public partial class Todo
{
    public int id { get; set; }
    public string content { get; set; }
    public bool done { get; set; }
}

And i send this as json data to my controller as a patch request. This is mearly the action of toggeling a checkbox. I think it makes sence that i only want to sent that to my server, and not the entire model.

{ "id":1, "done" : true }

What does my WebApi controller need to look like in order to correctly process this, simple, json patch request ? Should i be using web api for this, or should i use a more rpc styled approach with mvc ?

It seems like a very basic thing to do, but i can't seem to get it right ! I think i might need to use a different parameter in my controller method, but i'm not sure.

Thank you for your time.

like image 688
Willem D'Haeseleer Avatar asked Apr 26 '12 11:04

Willem D'Haeseleer


People also ask

How do I use Web API PATCH?

HTTP POST can support partial updates to a resource. But there is a separate PATCH method. This new HTTP method, PATCH, modifies an existing HTTP resource. The call to the Patch method is sufficient to partially update the corresponding properties of the Employee object in the list.

How do I send a browser PATCH request?

To send a PATCH request to the server, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The Content-Type request header must indicate the data type in the body. In this PATCH request example, we send JSON to the ReqBin echo endpoint to update the data on the server.

What is PATCH method in API?

PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.


1 Answers

You can find PATCH feature in the OData pre-release Nuget package: Microsoft.AspNet.WebApi.OData.

Information how you can use it to create an action for handling PATCH can be found in the Partial Updates (PATCH requests) section of the blog post about OData support in ASP.NET Web API.

like image 190
Maksymilian Majer Avatar answered Oct 06 '22 03:10

Maksymilian Majer