wcf - How can I execute code before (EVERY) Web API action? -
i have web api interface i'm trying adapt multi-tenant architecture. previously, had wcf mode whereby passed parameter, client id, service, stored use in code later. meant client id didn't have first parameter passed every call.
i'd same web api, i.e., rather having:
getdocument(int clientid, int documentid) getdefault(int clientid) getimage(int clientid, int imageid)
have just:
getdocument(int documentid) getdefault() getimage(int imageid)
but need way following:
- get clientid route
- put value state object i've got
all before call executes. i'm kind of thinking route rewritten - i'm fine route having have client id in it, not api. call getdefault might like:
/document/getdefault/1
while api is
getdefault()
thoughts? tia.
one approach custom actionfilter. see here, although it's mvc concept identical webapi:
asp.net mvc provides action filters executing filtering logic either before or after action method called. action filters custom attributes provide declarative means add pre-action , post-action behavior controller's action methods.
for example:
public class myactionfilter : actionfilterattribute { public override void onactionexecuting(httpactioncontext actioncontext) { //.... } public override void onactionexecuted(httpactionexecutedcontext actionexecutedcontext) { //.... } } and use decorate api controllers/actions:
[myactionfilter] public ienumerable<string> get() { return new string[] { "value1", "value2" }; }
Comments
Post a Comment