asp.net - 400 Bad Request for OData -
i created .net 4.5 web project using visual studio 2012 test out using odata following this tutorial. however, whenever send following request fiddler:
put http://localhost:14116/odata/products(5) http/1.1 host: localhost:14116 accept: application/json content-type: application/json content-length: 67 { "name" : "apple //e", "price" : "1298", "category" : "computer" }
i following response:
http/1.1 400 bad request cache-control: no-cache pragma: no-cache expires: -1 server: microsoft-iis/8.0 x-aspnet-version: 4.0.30319 x-sourcefiles: =?utf-8?b?yzpcdxnlcnnczgxhdmvsbgvczg9jdw1lbnrzxhzpc3vhbcbzdhvkaw8gmjaxmlxqcm9qzwn0c1xp rgf0yvrlc3qzxe9eyxrhvgvzddncb2rhdgfcuhjvzhvjdhmonsk=?= x-powered-by: asp.net date: wed, 01 apr 2015 14:37:41 gmt content-length: 0
what missing? (stack overflow removing 2 empty lines in http response.)
this request works fine:
get http://localhost:14116/odata/products?$filter=price+lt+500 http/1.1 host: localhost:14116 accept: application/json
here's controller:
public class productscontroller : odatacontroller { static list<product> s_products; static productscontroller() { s_products = new list<product>(); s_products.add( new product { id = 1 , name = "nexus 7" , price = 179 , category = "tablet" } ); } // odata/products [queryable] public iqueryable<product> getproducts() { return s_products.asqueryable(); } // odata/products(5) [queryable] public singleresult<product> getproduct([fromodatauri] int key) { return singleresult.create(s_products.asqueryable().where(product => product.id == key)); } // put odata/products(5) public ihttpactionresult put([fromodatauri] int key, product product) { if (!modelstate.isvalid) { return badrequest(modelstate); } if (key != product.id) { return badrequest(); } var previous = s_products.singleordefault(p => p.id == key); if (previous == null) { return notfound(); } previous.category = product.category; previous.name = product.name; previous.price = product.price; return updated(product); } // post odata/products public ihttpactionresult post(product product) { if (!modelstate.isvalid) { return badrequest(modelstate); } s_products.add(product); return created(product); } // patch odata/products(5) [acceptverbs("patch", "merge")] public ihttpactionresult patch([fromodatauri] int key, delta<product> patch) { if (!modelstate.isvalid) { return badrequest(modelstate); } product product = s_products.singleordefault(p => p.id == key); if (product == null) { return notfound(); } patch.patch(product); return updated(product); } // delete odata/products(5) public ihttpactionresult delete([fromodatauri] int key) { product product = s_products.singleordefault(p => p.id == key); if (product == null) { return notfound(); } s_products.remove(product); return statuscode(httpstatuscode.nocontent); } protected override void dispose(bool disposing) { base.dispose(disposing); } }
Comments
Post a Comment