c# - Web Api controller thinks it is duplicated -
i have webforms app, has web api controllers in integrations. 2 working fine. today wanted add controller.
should simple enough, add new web api controller, add little bit of code it, and:
namespace myapp.web.app_code { public class authexportcontroller : apicontroller { public ienumerable<string> get() { return new string[] { "value1", "value2" }; } } }
and when call @ url :
http://localhost:30978/myapp/api/authexport
i error:
<error> <message>an error has occurred.</message> <exceptionmessage>multiple types found match controller named 'authexport'. can happen if route services request ('api/{controller}/{id}') found multiple controllers defined same name differing namespaces, not supported. request 'authexport' has found following matching controllers: myapp.web.app_code.authexportcontroller myapp.web.app_code.authexportcontroller</exceptionmessage> <exceptiontype>system.invalidoperationexception</exceptiontype> <stacktrace> @ system.web.http.dispatcher.defaulthttpcontrollerselector.selectcontroller(httprequestmessag e request) @ system.web.http.dispatcher.httpcontrollerdispatcher.sendasyncinternal(httprequestmessage request, cancellationtoken cancellationtoken) @ system.web.http.dispatcher.httpcontrollerdispatcher.sendasync(httprequestmessage request, cancellationtoken cancellationtoken)</stacktrace> </error>
as can see, complaining 2 controllers same name, exact same controller. other controllers work fine, particular one.
if helps anything, routing code in global.asax
protected void application_start(object sender, eventargs e) { routetable.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = system.web.http.routeparameter.optional } ); globalconfiguration.configuration.formatters.clear(); globalconfiguration.configuration.formatters.add(new xmlmediatypeformatter()); }
driving me mad!
update:
ok managed fix it, don't understand why, if explain me. compared working controllers non working one. exact same signatures exactly, nothing different, same using statements, same inheritance etc etc. 1 thing did notice build action on file. working file has build action of "content" , non working has build action of "compile" . change non working 1 "content" , works.
so more confused :) happy works, don't black magic in systems
tl;dr:
delete bin
folder in projects' file directory , build project again.
explanation:
i encountered error while renaming existing project , changing namespaces , happened:
let's projects' name firstname
, wanted rename secondname
.
- i built project compiled , generated
bin/debug/firstname.dll
files - i renamed project
secondname
- i refactored namespace
- built , ran project
- this generated
bin/debug/secondname.dll
when project ran, iis express took of .dll
files , loaded them memory, happened include both firstname.dll
, secondname.dll
.
as might understood, firstname.dll
, secondname.dll
had same classes , methods, different namespaces! therefore, both of dlls loaded memory iis, webapi routing mechanism finding both of namespaces same controllers , action names.
and there have it. no voodoo magic after :)
Comments
Post a Comment