.net - How to invoke one controller method from another given only the uri -


for simplicity consider person resource such as

{"name":"fred flintston", "worksat":    {"href":"api/sites?cn=slate%20rock%20and%20gravel%20company"} } 

when receive on post peoplecontroller need site resource worksat.href.

what invoke correct on sitescontroller leveraging routing engine knows how parse uri , invoke correct method.

i have seen 1 suggestion here seems rather heavy handed, , i'm not @ sure how allow authorization has happened carry through.

you can try following, if describe in detail want achieve, might able find better solution. mvc based, should work web api too.

var baseurl = string.format("{0}://{1}{2}", request.url.scheme, request.url.authority, url.content("~")); var requesturl = new uri(baseurl + "home/index?i=42");  //get method info var httpcontext = new httpcontextwrapper(new httpcontext(new httprequest("/", requesturl.absoluteuri, ""), new httpresponse(new stringwriter()))); var requestcontext = new requestcontext(httpcontext, routetable.routes.getroutedata(httpcontext)); var controllertype = assembly.getexecutingassembly().gettypes().firstordefault(x => x.name == requestcontext.routedata.values["controller"].tostring() + "controller"); var controllercontext = new controllercontext(requestcontext, activator.createinstance(controllertype) controllerbase); var controllerdescriptor = new reflectedcontrollerdescriptor(controllertype); var actiondescriptor = controllerdescriptor.findaction(controllercontext, controllercontext.routedata.values["action"].tostring()); var methodinfo = (actiondescriptor reflectedactiondescriptor).methodinfo;  //parse query string var qscoll = httputility.parsequerystring(requesturl.query); //and use item in int when calling action var ret = methodinfo.invoke(activator.createinstance(controllertype), new object[] { int.parse(qscoll[0]) }); 

the parameter passing baked in, you'd need create correctly ordered parameter array, based on parameter names, , types.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -