Can i use http patch with the web api to update only certain fields from my entity model ?
Can someone possibly provide some pseudo code or maybe a working example ?
If i can't use patch what is the common alternative aproach to update only certain fields with the web api ?
Thank you aliostad, i took a look at those documents, i'm not sure however how to exactly implement that.
Could you provide some very simple pseude code , i am not sure what the api controller method should look like.
Thank you for your exmple, i'm still not completly clear on how to use this. Allow me to demonstrate.
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 }
This is my controller
[AcceptVerbs("PATCH")]
public string Patch( Todo todo )
{
/*
How do i determine wich JSON values where submitted ?
How do i send only the submitted JSON values to the database ?
*/
}
It seems like a very basic thing to do, but i can't seem to get it right !
Thank you for your time.
Helmus
Member
7 Points
19 Posts
http patch to only update certain fields
Apr 26, 2012 10:22 AM|LINK
Can i use http patch with the web api to update only certain fields from my entity model ?
Can someone possibly provide some pseudo code or maybe a working example ?
If i can't use patch what is the common alternative aproach to update only certain fields with the web api ?
patch update
aliostad
Member
228 Points
55 Posts
Re: http patch to only update certain fields
Apr 26, 2012 11:11 AM|LINK
According to this http://aspnetwebstack.codeplex.com/workitem/3 , Web API already supports PATCH verb.
According to http://aspnetwebstack.codeplex.com/discussions/350398 , Web API supports AcceptVerbsAttribute so you can define PATCH using the attribute.
This works (although actually does not what PATCH is suppose to do but my point is PATCH verb already supported):
[AcceptVerbs("PATCH")]
public string Patch()
{
return "Patch!";
}
In terms of functionality, you need to:
Helmus
Member
7 Points
19 Posts
Re: http patch to only update certain fields
Apr 26, 2012 11:16 AM|LINK
Thank you aliostad, i took a look at those documents, i'm not sure however how to exactly implement that.
Could you provide some very simple pseude code , i am not sure what the api controller method should look like.
patch update
aliostad
Member
228 Points
55 Posts
Re: http patch to only update certain fields
Apr 26, 2012 11:16 AM|LINK
Please see my updates.
Helmus
Member
7 Points
19 Posts
Re: http patch to only update certain fields
Apr 26, 2012 11:39 AM|LINK
Thank you for your exmple, i'm still not completly clear on how to use this. Allow me to demonstrate.
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 }This is my controller
[AcceptVerbs("PATCH")] public string Patch( Todo todo ) { /* How do i determine wich JSON values where submitted ? How do i send only the submitted JSON values to the database ? */ }It seems like a very basic thing to do, but i can't seem to get it right !
Thank you for your time.
aliostad
Member
228 Points
55 Posts
Re: http patch to only update certain fields
Apr 26, 2012 12:29 PM|LINK
ASP.NET Web API seems to be missing `UpdateModel`, `TryUpdateModel`, etc.
In ASP.NET MVC, you could use them to achieve the desired effect. I have created a work item in ASP.NET Web Stack which you can vote for.
Work item: http://aspnetwebstack.codeplex.com/workitem/102