web services - Getting the "no type was found that matches the controller named" error message during Ajax Request -
i've seen lot of topics this, unfortunately believe each case different case (or of them), , love experts opinion case in particular since cannot make code work after reading through of other topics.
situation: using ajax request call in jquery webservice method have created in webapi project mvc 4 application.
my webservice controller class looks default, this:
public class additionalinfocontroller : apicontroller { //get api/additionalinfo public ienumerable<string> get() { return new string[] { "value1", "value2" }; } //get api/additionalinfo/5 public string get(int id) { return "value"; } //put api/additionalinfo/5 public void put(int id) { string test = ""; } }
my ajax request jquery looks this:
function getadditionalinfo(obj) { var request = jquery.ajax({ url: "/api/additionalinfo/get", type: "get", data: { id: obj.id }, datatype: "json", async: false, beforesend: function () { }, complete: function () { } }) .done(function (a,b,c) { alert("additional info retrieved successfully!"); }) .fail(function (a,b,c) { alert("an error happened while trying additional info!"); }); }
my webapiconfig file looks this:
public static class webapiconfig { public static void register(httpconfiguration config) { config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional } ); } }
and last not least, problem: error message keeps appearing when browse returned data variable in .fail , written:
"{ "message":"no http resource found matches request uri 'http://localhost:59096/api/additionalinfo/get?id=1'.", "messagedetail":"no type found matches controller named 'additionalinfo'." }"
i appreciate if me possible. in advance!
best regards,
mad
looking @ error looks web api unable find controller 'type' additionalinfo. web api uses assemblies resolver scan through assemblies , finds out controller types. in case reason unable find 'additionalinfo' controller because has problem loading assembly having controller.
try following , see if there errors logged in eventlog. if notice errors should check if controllers present in assemblies.
make following change in web.config view errors in eventlog
<system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="mylistener" type="system.diagnostics.eventlogtracelistener" initializedata="webapidiagnostics" /> </listeners> </trace> </system.diagnostics>
in webapiconfig.cs, can following:
iassembliesresolver assembliesresolver = config.services.getassembliesresolver(); icollection<assembly> assemblies = assembliesresolver.getassemblies(); stringbuilder errorsbuilder = new stringbuilder(); foreach (assembly assembly in assemblies) { type[] exportedtypes = null; if (assembly == null || assembly.isdynamic) { // can't call getexportedtypes on dynamic assembly continue; } try { exportedtypes = assembly.getexportedtypes(); } catch (reflectiontypeloadexception ex) { exportedtypes = ex.types; } catch (exception ex) { errorsbuilder.appendline(ex.tostring()); } } if (errorsbuilder.length > 0) { //log errors event log trace.traceerror(errorsbuilder.tostring()); }
btw, of above code defaulthttpcontrollertypesresolver web api uses resolve controller types. http://aspnetwebstack.codeplex.com/sourcecontrol/latest#src/system.web.http/dispatcher/defaulthttpcontrollertyperesolver.cs
edited: 1 more scenario hit problem if controller nested inside class. bug fixed later though.
Comments
Post a Comment