asp.net web api - Exception in HttpControllerDispatcher -
in webapiconfig::register(...) replaced httpcontrollerselector own controller selector. when fire off post request selectcontroller member correctly called , return controllerdescriptor correct type of controller. httpcontrollerdispatcher raising exception saying "the given not present in dictionary." has idea how debug such error?
the complete exception message is:
the given key not present in dictionary.","exceptiontype":"system.collections.generic.keynotfoundexception","stacktrace":" @ system.collections.generic.dictionary`2.get_item(tkey key)\r\n @ system.web.http.controllers.apicontrolleractionselector.actionselectorcacheitem.findactionmatchrequiredrouteandqueryparameters(ienumerable`1 candidatesfound)\r\n @ system.web.http.controllers.apicontrolleractionselector.actionselectorcacheitem.findmatchingactions(httpcontrollercontext controllercontext, boolean ignoreverbs)\r\n @ system.web.http.controllers.apicontrolleractionselector.actionselectorcacheitem.selectaction(httpcontrollercontext controllercontext)\r\n @ system.web.http.controllers.apicontrolleractionselector.selectaction(httpcontrollercontext controllercontext)\r\n @ system.web.http.apicontroller.executeasync(httpcontrollercontext controllercontext, cancellationtoken cancellationtoken)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__1.movenext()" and here controller selector:
public class namespace_http_controller_selector : ihttpcontrollerselector { private readonly httpconfiguration _configuration; private readonly lazy<dictionary<string, httpcontrollerdescriptor>> _controller; public namespace_http_controller_selector(httpconfiguration config) { _configuration = config; _controller = new lazy<dictionary<string, httpcontrollerdescriptor>>(initialize_controller_dictionary); } public httpcontrollerdescriptor selectcontroller(httprequestmessage request) { var route_data = request.getroutedata(); if(route_data == null) { throw new httpresponseexception(httpstatuscode.notfound); } var controller_name = get_controller_name(route_data); if(controller_name == null) { throw new httpresponseexception(httpstatuscode.notfound); } var name_space = get_version(route_data); if (name_space == null) { throw new httpresponseexception(httpstatuscode.notfound); } var controller_key = string.format(cultureinfo.invariantculture, "{0}.{1}", name_space, controller_name); httpcontrollerdescriptor descriptor; if(_controller.value.trygetvalue(controller_key, out descriptor)) { return descriptor; } throw new httpresponseexception(httpstatuscode.notfound); } public idictionary<string, httpcontrollerdescriptor> getcontrollermapping() { return _controller.value; } private dictionary<string, httpcontrollerdescriptor> initialize_controller_dictionary() { var dictionary = new dictionary<string, httpcontrollerdescriptor>(stringcomparer.ordinalignorecase); var assemblies_resolver = _configuration.services.getassembliesresolver(); var controller_resolver = _configuration.services.gethttpcontrollertyperesolver(); var controller_types = controller_resolver.getcontrollertypes(assemblies_resolver); foreach(var ct in controller_types) { var segments = ct.namespace.split(type.delimiter); var controller_name = ct.name.remove(ct.name.length - defaulthttpcontrollerselector.controllersuffix.length); var controller_key = string.format(cultureinfo.invariantculture, "{0}.{1}", segments[segments.length - 1], controller_name); if(dictionary.keys.contains(controller_key) == false) { dictionary[controller_key] = new httpcontrollerdescriptor(_configuration, ct.name, ct); } } return dictionary; } private t get_route_variable<t>(ihttproutedata route_data, string name) { object result; if(route_data.values.trygetvalue(name, out result)) { return (t)result; } return default(t); } private string get_controller_name(ihttproutedata route_data) { var subroute = route_data.getsubroutes().firstordefault(); if( subroute == null ) { return null; } var data_token_value = subroute.route.datatokens.first().value; if(data_token_value == null) { return null; } var controller_name = ((httpactiondescriptor[])data_token_value).first().controllerdescriptor.controllername.replace("controller", string.empty); return controller_name; } private string get_version(ihttproutedata route_data) { var sub_route_data = route_data.getsubroutes().firstordefault(); if(sub_route_data== null) { return null; } return get_route_variable<string>(sub_route_data, "apiversion"); } }
this because not setting subroute data in request before returning descriptor.
httpcontrollerdescriptor descriptor; if(_controller.value.trygetvalue(controller_key, out descriptor)) { var subroutes = route_data.getsubroutes(); ienumerable<ihttproutedata> filteredsubroutes = subroutes.where(attrroutedata => { httpcontrollerdescriptor currentdescriptor = ((httpactiondescriptor[])route_data.route.datatokens["actions"]).first().controllerdescriptor; return currentdescriptor != null && currentdescriptor.controllername.equals(descriptor.controllername, stringcomparison.ordinalignorecase); }); route_data.values["ms_subroutes"] = filteredsubroutes.toarray(); return descriptor; }
Comments
Post a Comment