android - Passing variables to IHttpActionResult Methods -
i want consume webapi in android below (in newproductactivity.java):
protected string doinbackground(string... params) { list<namevaluepair> args = new arraylist<namevaluepair>(); args.add(new basicnamevaluepair("name", edittextname.gettext().tostring())); args.add(new basicnamevaluepair("price", edittextprice.gettext().tostring())); args.add(new basicnamevaluepair("description", edittextdescription.gettext().tostring())); jsonhttpclient jsonhttpclient = new jsonhttpclient(); product product = (product) jsonhttpclient.postparams(serviceurl.product, args, product.class); intent intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(intent); finish(); return null; }
in web api assigned variables parameters in productscontroller.cs below:
[httppost] //[responsetype(typeof(product))] public ihttpactionresult insertproduct(string name, decimal price, string description) { product product = new product() { name = name, price = price, description = description }; if (!modelstate.isvalid) { return badrequest(modelstate); } db.products.add(product); db.savechangesasync(); return ok(product.id); }
when test api post in fiddler getting 405 status code.i have tried using:
//[route("api/products/{name}/{price}/{description}")] // post: api/products [httppost] //[responsetype(typeof(product))] public ihttpactionresult postproduct(string name, decimal price, string description) {
as original implementation example im following (http://hintdesk.com/how-to-call-asp-net-web-api-service-from-android) still 405:
[httppost] //[responsetype(typeof(product))] public product post(string name, decimal price, string description) { product product = new product() { name = name, price = price, description = description };
if want post mvc controller this:
make complex model (class) properties this:
class myrequest{ public string name {get;set}; public decimal price{get;set}; public string description{get;set}; }
and change controller this:
public product post(myrequest request)
don't forget change android code sends complex object.
Comments
Post a Comment