c# - How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 -
how have route points 2 different controller end points accepts different arguments in web api 2 have 2 different end points declared in controller , rest perspective have use alpha/{aplhaid}/beta format both end points ,
[authorize] [httppost] [route("alpha/{aplhaid}/beta")] public async task<httpresponsemessage> createalpha(beta beta, string projectid, [fromheader] revisionheadermodel revision) [authorize] [httppost] [route("alpha/{aplhaid}/beta")] public async task<httpresponsemessage> createalpha(list<beta> betas, string projectid, [fromheader] revisionheadermodel revision)
is possible use same router different parameters points 2 different end points in web api 2?
if need have same route , same actionname, ihttpactionselector
.
public class customactionselector : apicontrolleractionselector, ihttpactionselector { public new httpactiondescriptor selectaction(httpcontrollercontext controllercontext) { var context = httpcontext.current; // read content. better way of doing it? var stream = new streamreader(context.request.inputstream); var input = stream.readtoend(); var array = new javascriptserializer().deserialize<list<string>>(input); if (array != null) { // it's array //todo: choose action. } else { // it's not array //todo: choose action. } // default. var action = base.selectaction(controllercontext); return action; } public override ilookup<string, httpactiondescriptor> getactionmapping(httpcontrollerdescriptor controllerdescriptor) { var lookup = base.getactionmapping(controllerdescriptor); return lookup; } }
in webapiconfig:
config.services.replace( typeof(ihttpactionselector), new customactionselector());
example controller:
public class foocontroller: apicontroller { [httppost] public string post(string id) { return "string"; } [httppost] public string post(list<string> id) { return "some list"; } }
the solution has big downsides, if ask me. first of, should solution using customactionselector
when needed. not controllers create overhead each request.
i think should reconsider why need 2 have identical routes. think readability suffering if same route accepts different arguments. that's opinion.
i use different routes instead.
Comments
Post a Comment