asp.net mvc - RESTful Controllers with Different Http Methods, But the Same Parameters -
let's have controller handles crud scenario 'home'. this:
[httpget] public actionresult index(int? homeid) { home home = homerepo.gethome(homeid.value); return json(home, jsonrequestbehavior.allowget); }
so far good. add post action adding new ones.
[httppost] public actionresult index(home home) { //add new home db return json(new { success = true }); }
awesome. when use same scheme handle puts (updating existing home)...
[httpput] public actionresult index(home home) { //update existing home in db return json(new { success = true }); }
we run problem. method signatures post , put identical, of course c# doesn't like. try few things, adding bogus parameters signature, or changing method names directly reflect crud. hacky or undesirable, though.
what best practice going preserving restful, crud style controllers here?
this best solution know of:
[httpput] [actionname("index")] public actionresult indexput(home home) { ... }
basically actionnameattribute
created deal these scenarios.
Comments
Post a Comment