No type was found that matches the controller named 'User'

87,405

Solution 1

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.

Solution 2

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!

Solution 3

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}'

Solution 4

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

set this:

class DocumentAPIController : ApiController
{
}

to:

public class DocumentAPIController : ApiController
{
}

Solution 5

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

Share:
87,405
elad
Author by

elad

Updated on July 09, 2022

Comments

  • elad
    elad almost 2 years

    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?

    • Darin Dimitrov
      Darin Dimitrov almost 11 years
      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?
    • elad
      elad almost 11 years
      Yes I am using ASP.NET Web API and you can see the routing definition above
    • Darin Dimitrov
      Darin Dimitrov almost 11 years
      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.
    • elad
      elad almost 11 years
      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'"
  • Ravi Khambhati
    Ravi Khambhati about 8 years
    In my case I updated route template from "api/{controller}/{id}" to "api/{controller}/{action}/{id}" and it worked.
  • Shaakir
    Shaakir almost 8 years
    Thanks... followed this example and it made no sense.. the routing that is... asp.net/web-api/overview/getting-started-with-aspnet-web-api‌​/…
  • Hinrich
    Hinrich over 7 years
    Had exactly the same issue with interfering configs. Thank you!
  • JD - DC TECH
    JD - DC TECH over 7 years
    In my case was I don't had name 'Controller' in my API class. Convention over configuration.
  • zeroflaw
    zeroflaw about 6 years
    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... >.<
  • Corey Alix
    Corey Alix over 5 years
    It also needs to be public.
  • Scott Fraley
    Scott Fraley over 4 years
    Pretty sure this is to do with that whole Convention over Configuration thing. :)
  • drewmerk
    drewmerk about 4 years
    Don't forget to check that you have "Controller" spelled correctly in your class name!
  • Carlos Toledo
    Carlos Toledo over 3 years
    Perfect!! Can you add than in the mapping we should put [controller ="User"] instead of [controller ="UserController"]