I have a webforms app, that has some Web Api controllers in it for integrations. Two are working just fine. But Today I wanted to add another controller.
Should be simple enough, I add a new Web Api controller, add a little bit of code to it, and:
namespace MyApp.Web.App_Code
{
public class AuthExportController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
and when i call it at this url :
http://localhost:30978/MyApp/api/AuthExport
I get this error:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Multiple types were found that match the controller named
'AuthExport'. This can happen if the route that services this request
('api/{controller}/{id}') found multiple controllers defined with the same name but
differing namespaces, which is not supported. The request for 'AuthExport' has found
the following matching controllers: MyApp.Web.App_Code.AuthExportController
MyApp.Web.App_Code.AuthExportController</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace> at
System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessag
e request) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage
request, CancellationToken cancellationToken) at
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage
request, CancellationToken cancellationToken)</StackTrace>
</Error>
As you can see, it is complaining about 2 controllers with the same name, but its the exact same controller. My other controllers work fine, just this particular one.
If it helps anything, this is my 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 I managed to fix it, but I don't understand why, so if someone could explain it to me. I compared my working controllers with my non working one. Exact same signatures exactly, nothing different, same using statements, same inheritance etc etc. One thing I did notice is the build action on the file. Working file has a build action of "Content" and the non working has a build action of "Compile" . I change my non working one to "Content" and it works.
So now I am even more confused :) Happy that it works, but I don't like black magic in my systems
TL;DR:
Delete bin
folder in your project's file directory and build your project again.
Explanation:
I just encountered this error while renaming an existing project and changing namespaces for it and this is what happened:
Let's say projects' name was FirstName
and I wanted to rename it to SecondName
.
bin/Debug/FirstName.dll
filesSecondName
bin/Debug/SecondName.dll
When project was ran, IIS Express simply took all of the .dll
files and loaded them into memory, which happened to include both FirstName.dll
and SecondName.dll
.
As you might already understood, FirstName.dll
and SecondName.dll
had the same classes and methods, but with a different namespaces!
Therefore, as both of DLLs were loaded into memory by IIS, WebApi routing mechanism was finding both of namespaces with same controllers and action names.
And there you have it. No voodoo magic after all :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With