asp.net web api - How to extract a context from an URL before reaching the route engine in Web API 2 + Attribute Routing? -
i'm migrating web service pure c# / no framework web api 2. web service exposes movies objects (with properties title, actors, plot...) , these movies objects contains videocontents objects. these videocontents objects associated device and/or specific siteinstance (think of catalog example).
i have follow specific url model :
/api/movies/1234 = returns movie #1234 of contents /api/{device}/movies/1234 = returns movie #1234 contents matching device /api/{device}/{siteinstance}/movies/1234 = returns movie #1234 contents matching device , siteinstance.
i made first version "traditionnal" routing, had 3 routes defined :
"api/{controller}/{id}" "api/{device}/{controller}/{id}" "api/{device}/{instance}/{controller}/{id}"
and used iautofacactionfilter hook request, extract context , inject in controller. worked !
now, want migrate attributerouting because, well, seems "way go" , idea of routetemplate being near method future debugging ect... don't want repeat 3 times routes on each method of web service, i'm looking way "intercept" request before goes route engine, extract context, , returns clean url route engine. :
- request done /api/devicexyz/movies/1234
- request intercepted, "devicexyz" extracted , context object built / injected needs (my controller)
- /api/movies/1234 sent route engine
- request matched attributerouting : [route("api/movies/{id:int}")] , executed.
i'm looking forward ideas, if it's not way see it. think whole context parsing should not done route engine maybe i'm wrong.
thanks !
edit: forgot add don't want see these parameters in methods signatures. used build context object injected in controller constructor through autofac.
if want intercept every request of service can implement custom message handler.
i use:
httpselfhostconfiguration config = new httpselfhostconfiguration("http://127.0.0.1"); config.messagehandlers.add(new requestinterceptor());
with requestinterceptor class defined as:
public class requestinterceptor : delegatinghandler { public requestinterceptor () { } protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { // here can whatever want request. return base.sendasync(request, cancellationtoken); } }
this intercepts first before being routed.
also, may want review dependency injection http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver handle device specific contexts.
Comments
Post a Comment