58

I'm trying to navigate to a page which its URL is in the following format: localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx

I've added a new route in the RouteConfig.cs file and so my RouteConfig.cs looks like this:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "VerifyEmail",
            url: "User/{id}/VerifyEmail",
            defaults: new { controller = "User", action = "VerifyEmail" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index",
                id = UrlParameter.Optional }
        );
    }
}

Unfortunately, when trying to navigate to that URL I get this page:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller named 'User'.
    </MessageDetail>
</Error>

and here is my UserController:

public class UserController : Controller
{

    // GET      /User/{id}/VerifyEmail
    [HttpGet]
    public ActionResult VerifyEmail(string id, string secretKey)
    {
        try
        {
            User user = UsersBL.Instance.Verify(id, secretKey);
            //logger.Debug(String.Format("User %s just signed-in in by email.",
                user.DebugDescription()));
        }
        catch (Exception e)
        {
            throw new Exception("Failed", e);
        }
        return View();
    }
}

Please tell me what am I doing wrong?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
elad
  • 738
  • 1
  • 6
  • 17
  • 3
    It's strange that the error is returned as XML document. As if you were using ASP.NET Web API which is hijacking this route. Are you using ASP.NET Web API? And if yes, how does its routing definition looks like? – Darin Dimitrov Jul 23 '13 at 15:06
  • 1
    Yes I am using ASP.NET Web API and you can see the routing definition above – elad Jul 24 '13 at 09:55
  • 9
    No, wait a second. You seem to be confusing ASP.NET Web API and ASP.NET MVC. Those are 2 completely different technologies. What you have shown in your question is an ASP.NET MVC route definitions and controllers. ASP.NET Web API uses entirely different stack. ASP.NET Web API controllers derive from `ApiController` and not from `Controller` and do not return ActionResults contrary to what is shown in your question. – Darin Dimitrov Jul 24 '13 at 11:07
  • 3
    I know that. When you create a WEB API application you get HomeController which inherits from Controller and not ApiController. My problem is not with my WEB API controllers. I get them all to work fine. The problem is that except from the Index method from the HomeController which I can access, all other methods aren't recognized and I get the exception "No type was found that matches the controller named 'User'" – elad Jul 25 '13 at 11:57

16 Answers16

83

In my case, the controller was defined as:

    public class DocumentAPI : ApiController
    {
    }

Changing it to the following worked!

    public class DocumentAPIController : ApiController
    {
    }

The class name has to end with Controller!

Edit: As @Corey Alix has suggested, please make sure that the controller has a public access modifier; non-public controllers are ignored by the route handler!

Raghu
  • 1,415
  • 13
  • 18
52

In my case after spending almost 30 minutes trying to fix the problem, I found what was causing it:

My route defined in WebApiConfig.cs was like this:

config.Routes.MapHttpRoute(
    name: "ControllersApi",
    routeTemplate: "{controller}/{action}"
);

and it should be like this:

config.Routes.MapHttpRoute(
    name: "ControllersApi",
     routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
);

as you see it was interfering with the standard route defined in RouteConfig.cs.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 4
    In my case I updated route template from "api/{controller}/{id}" to "api/{controller}/{action}/{id}" and it worked. – Ravi Khambhati Mar 09 '16 at 18:30
  • 1
    Thanks... followed this example and it made no sense.. the routing that is... http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms – Shaakir Jul 19 '16 at 10:05
  • 3
    Had exactly the same issue with interfering configs. Thank you! – Hinrich Nov 03 '16 at 11:01
  • 5
    In my case was I don't had name 'Controller' in my API class. Convention over configuration. – JD - DC TECH Nov 17 '16 at 14:09
  • 1
    I visit this post again and again. On and off hit this issue. But this time is a very rare case encounter. Originally I set the Project Output path to bin\, it works. But after i set to bin\Debug or bin\Release, somehow it doesn't work anymore (no idea...) Remove the bin file and reset to \bin will work... >. – zeroflaw Apr 04 '18 at 08:01
  • 2
    Don't forget to check that you have "Controller" spelled correctly in your class name! – drewmerk Mar 12 '20 at 19:54
29

In my case I was using Web API and I did not have the public defined for my controller class.

Things to check for Web API:

  • Controller Class is declares as public
  • Controller Class implements ApiController : ApiController
  • Controller Class name needs to end in Controller
  • Check that your url has the /api/ prefix. eg. 'host:port/api/{controller}/{actionMethod}'
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • 1
    Perfect!! Can you add than in the mapping we should put [controller ="User"] instead of [controller ="UserController"] – Carlos Toledo Sep 29 '20 at 18:38
24

Another solution could be to set the controllers class permission to public.

set this:

class DocumentAPIController : ApiController
{
}

to:

public class DocumentAPIController : ApiController
{
}
ElRaph
  • 241
  • 2
  • 4
10

In my case I wanted to create a Web API controller, but, because of inattention, my controller was inherited from Controller instead of ApiController.

Sergey_T
  • 538
  • 9
  • 14
7

In my case, the routing was defined as:

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{*catchall}",
            defaults: new { controller = "WarehouseController" }

while Controller needs to be dropped in the config:

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{*catchall}",
            defaults: new { controller = "Warehouse" }
Juliusz
  • 2,096
  • 2
  • 27
  • 32
4

In my case I was seeing this because I had two controllers with the same name:

One for handling Customer orders called CustomersController and the other for getting events also called CustomersController

I had missed the duplication, I renamed the events one to CustomerEventsController and it worked perfectly

Robert
  • 41
  • 1
3

Faced the same problem. Checked all the answers here but my problem was in namespacing. Routing attributes exists in System.Web.Mvc and in System.Web.Http. My usings included Mvc namespace and it was the reason. For webapi u need to use System.Net.Http.

3

In my case it was a case of over-aggressive caching by the WebHostHttpControllerTypeResolver.

Fix:

  1. Delete all files (or in my case just any files named "MS-ApiControllerTypeCache.xml") under this path:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root

  2. Restart the app pool

credit: https://sitecore.stackexchange.com/questions/9897/webapi-controllers-not-being-found-in-sitecore-8-2

sitecorepm
  • 101
  • 1
  • 2
1

Experienced this similar issue. We are dealing with multiple APIs and we were hitting the wrong port number and getting this error. Took us forever to realize. Make sure the port of the api you are hitting is the correct port.

Erkin Djindjiev
  • 517
  • 4
  • 7
1

I have also faced the same problem. I searched a lot and found that the class level permission is needed. by default, the class permission level is internal so I thought that it won't affect the program execution. But it got affected actually, you should give your class permission as public so that, you won't face any problem.

And one more. if it is webapi project, your webapirouteconfig file will overwrite the routeconfig.cs file settings. So update the webapi routeconfig file as well to work properly.

Gopi P
  • 525
  • 9
  • 19
1

In my solution, I have a project called "P420" and into other project I had a P420Controller.

When .NET cut controller name to find route, conflict with other project, used as a library into.

Hope it helps.

Andre Mesquita
  • 879
  • 12
  • 25
1

In my case I was calling the APi like

http://locahost:56159/api/loginDataController/GetLoginData

while it should be like

http://locahost:56159/api/loginData/GetLoginData

removed Controller from URL and it started working ...

Peace!

Azhar
  • 20,500
  • 38
  • 146
  • 211
0

In my solution, when I added the my new Controller to the project, the wizard asked me if I want to set the location of the controller into the App_Code folder. The wizard warned me, if I do not locate it into the the App_Code folder, the controller type won't be found. But I didn't read the whole warning, because I wanted to locate the file to elsewhere.. so that's why it didn't work for me.

After I added a new controller and let it to be in the App_Code by default, everything worked.

Bene
  • 81
  • 1
  • 4
0

And one more answer to this for good measure...

In my case another project had been accidentally added as a reference by someone which brought in all of that project's controllers and causing route conflicts. Removing it and moving the code that was needed from it to a better place where it could be referenced without bringing in all of the controllers was the solution.

Darinth
  • 511
  • 3
  • 14
0

Go to the location C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root and find withe the file name "MS-ApiControllerTypeCache.xml" and C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root

Delete all files named MS-ApiControllerTypeCache.xml from the root folder.

For my case, the issue ought to be addressed this way

Towhidul Islam Tuhin
  • 1,051
  • 13
  • 10