7

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Crudler
  • 2,194
  • 3
  • 30
  • 57
  • Do you use areas? If yes, [this](http://stackoverflow.com/questions/7842293/multiple-types-were-found-that-match-the-controller-named-home) might be related – Andrei Jun 27 '14 at 09:23
  • no areas. it is literally such a small vanilla app. a few web forms, and 3 controllers – Crudler Jun 27 '14 at 09:24
  • Have you done any assembly-level manipulations, e.g. renamed the project along the way? – Andrei Jun 27 '14 at 09:31
  • not on purpose or that i know of :) I updated the post with some new findings – Crudler Jun 27 '14 at 09:35
  • That's kind of strange. Since controllers are code files, they should have 'Compile' build action. Could you, just for an experiment, change all controllers to that and see what happens? – Andrei Jun 27 '14 at 09:49
  • if I take a working onw, and change it, i get the same error as above – Crudler Jun 27 '14 at 09:52
  • Will try to get back and explain what's going on here later when I get my hands on a VS to play with. Unless someone explains this in a meanwhile! – Andrei Jun 27 '14 at 09:58
  • I know this is old, but I want to answer the edit. The answer posted by @Dovydas Navickas below is most likely correct. The reason the project built when changing the build action is because by switching to "Content" the CS file is removed from the compiler, and is not built, and is not added to the project DLL, and no longer conflicts with the old DLLs in the bin folder. Sometimes I get weird errors like this and the first thing I do it delete the bin and obj folders from the project directory. Sometimes running a "Clean" will do the trick. – jwatts1980 Jan 10 '19 at 17:21

2 Answers2

33

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.

  • I built project which compiled everything and generated bin/Debug/FirstName.dll files
  • I renamed the project to SecondName
  • I refactored the namespace
  • Built and ran the project
  • This generated 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 :)

Dovydas Navickas
  • 3,533
  • 1
  • 32
  • 48
  • 2
    Accepting an answer as a correct one would be nice of yours :) – Dovydas Navickas Nov 30 '15 at 19:04
  • 1
    Great clear answer @Dovydas - saved me a heap of time troubleshooting. Crudler, please accept the answer or add a comment to why it's not working for you – Chris B Apr 06 '16 at 15:00
  • Upvote for suggestion to delete Bin, that is what I was going to suggest. – ablaze Aug 05 '16 at 01:22
  • Upvote for this, it just fixed the same problem I was having - not sure why you have not had this answer accepted! – Kirschstein Sep 28 '16 at 14:32
  • This drove me crazy whole day. Got struck in a same scenario of name changing and was getting this error until i came here. Thanks – Jay Dec 18 '17 at 21:33
0

I had to change names of references, projects and solution and after that I had this problem for days. When I looked for solution, I got the answer everywhere that remove your /bin folder contents and try rebuilding solution, I did that but this didn't solve my problem.I then created a separate solution added all these projects in that solution with bin folder empty, and build the solution still didn't solve my problem. Here are the things I had to do to get back on track:

  • First changed assembly names from Project/Properties/
  • Cleared /bin , /bin/debug folder

  • Cleared /obj/debug/(.dll, .cache, temporary created files) of each project.

Then rebuild the solution and finally got it solved. If removing only /bin doesn't work out then try this.

Jahanzeb Khan
  • 81
  • 1
  • 1
  • 5