ASP.Net Web API Action Result -
i'm building asp.net web api application , have following code...
public ihttpactionresult getcustomers() { var customers = context.customers.tolist(); return ok(customers); }
i'm using ok() method return customers because i'm using ihttpactionresult return type.
now if have following method
public void deletecustomer(int id) { var customerindb = context.customers.singleordefault(c => c.id == id); if (customerindb == null) { notfound(); } context.customers.remove(customerindb); context.savechanges(); }
can use notfound() method here when return type of actionmethod void???
void not have return type. can try call notfound()
, i'm not sure if compile - haven't tried. why don't go out of box ihttpactionresult
?
public ihttpactionresult deletecustomer(int id) { var customerindb = context.customers.singleordefault(c => c.id == id); if (customerindb == null) { return notfound(); } context.customers.remove(customerindb); context.savechanges(); return ok(customerindb); }
using ihttpactionresult
more elegant version. if id invalid, can safely exit method , tell calling client went wrong. if went well, you're giving client thumbs-up. if return deleted entity or empty ok()
should not matter @ point.
using void may or may not delete entity in data storage. client never know, because server not return response.
Comments
Post a Comment