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