-->

No type was found that matches the controller name

2019-02-01 04:35发布

问题:

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?

回答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.



回答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!



回答3:

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

set this:

class DocumentAPIController : ApiController
{
}

to:

public class DocumentAPIController : ApiController
{
}


回答4:

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


回答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.



回答6:

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


回答7:

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



回答8:

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.



回答9:

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.



回答10:

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.



回答11:

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.



回答12:

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.



回答13:

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!