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

asp.net Mvcasp.net Mvc-4asp.net Web-Apiasp.net Mvc-Routingasp.net Web-Api-Routing

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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 2 - asp.net Mvc

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 3 - asp.net Mvc

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 - asp.net Mvc

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

set this:

class DocumentAPIController : ApiController
{
}

to:

public class DocumentAPIController : ApiController
{
}

Solution 5 - asp.net Mvc

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

Solution 6 - asp.net Mvc

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

Solution 7 - asp.net Mvc

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

Solution 8 - asp.net Mvc

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.

Solution 9 - asp.net Mvc

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

Solution 10 - asp.net Mvc

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.

Solution 11 - asp.net Mvc

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.

Solution 12 - asp.net Mvc

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.

Solution 13 - asp.net Mvc

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.

Solution 14 - asp.net Mvc

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!

Solution 15 - asp.net Mvc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneladView Question on Stackoverflow
Solution 1 - asp.net MvcRaghuView Answer on Stackoverflow
Solution 2 - asp.net MvcLeniel MaccaferriView Answer on Stackoverflow
Solution 3 - asp.net MvcZapnologicaView Answer on Stackoverflow
Solution 4 - asp.net MvcElRaphView Answer on Stackoverflow
Solution 5 - asp.net MvcSergey_TView Answer on Stackoverflow
Solution 6 - asp.net MvcJuliuszView Answer on Stackoverflow
Solution 7 - asp.net MvcRobertView Answer on Stackoverflow
Solution 8 - asp.net MvcLeonid ErshovView Answer on Stackoverflow
Solution 9 - asp.net MvcsitecorepmView Answer on Stackoverflow
Solution 10 - asp.net MvcErkin DjindjievView Answer on Stackoverflow
Solution 11 - asp.net MvcGopi PView Answer on Stackoverflow
Solution 12 - asp.net MvcAndre MesquitaView Answer on Stackoverflow
Solution 13 - asp.net MvcBeneView Answer on Stackoverflow
Solution 14 - asp.net MvcAzharView Answer on Stackoverflow
Solution 15 - asp.net MvcDarinthView Answer on Stackoverflow