elad
elad

Reputation: 748

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

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?

Upvotes: 58

Views: 99431

Answers (17)

Ravi Nigam
Ravi Nigam

Reputation: 11

This is due to in absence of config.MapHttpAttributeRoutes(); , I also faced this and have resolved by calling this code in WebApiConfig.cs file

public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

Upvotes: 0

Towhidul Islam Tuhin
Towhidul Islam Tuhin

Reputation: 1249

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

Upvotes: 0

Sergey_T
Sergey_T

Reputation: 588

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

Upvotes: 10

Darinth
Darinth

Reputation: 510

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.

Upvotes: 0

sitecorepm
sitecorepm

Reputation: 101

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

Upvotes: 3

Raghu
Raghu

Reputation: 1443

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!

Upvotes: 84

Azhar
Azhar

Reputation: 20680

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!

Upvotes: 1

Leonid Ershov
Leonid Ershov

Reputation: 112

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.

Upvotes: 3

Bene
Bene

Reputation: 81

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.

Upvotes: 0

Andre Mesquita
Andre Mesquita

Reputation: 918

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.

Upvotes: 1

Gopi P
Gopi P

Reputation: 437

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.

Upvotes: 1

Zapnologica
Zapnologica

Reputation: 22556

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

Upvotes: 29

Robert
Robert

Reputation: 41

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

Upvotes: 4

Erkin Djindjiev
Erkin Djindjiev

Reputation: 577

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.

Upvotes: 1

ElRaph
ElRaph

Reputation: 241

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

set this:

class DocumentAPIController : ApiController
{
}

to:

public class DocumentAPIController : ApiController
{
}

Upvotes: 24

Juliusz
Juliusz

Reputation: 2105

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

Upvotes: 7

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

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.

Upvotes: 52

Related Questions