asp.net-mvc 找不到与名为“用户”的控制器匹配的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17811142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No type was found that matches the controller named 'User'
提问by elad
I'm trying to navigate to a page which its URL is in the following format: localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx
我正在尝试导航到其 URL 格式如下的页面:localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx
I've added a new route in the RouteConfig.csfile and so my RouteConfig.cslooks like this:
我在RouteConfig.cs文件中添加了一个新路由,所以我的RouteConfig.cs看起来像这样:
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:
不幸的是,当尝试导航到该 URL 时,我得到了这个页面:
<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:
这是我的 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?
请告诉我我做错了什么?
采纳答案by Leniel Maccaferri
In my case after spending almost 30 minutes trying to fix the problem, I found what was causing it:
就我而言,在花了近 30 分钟试图解决问题后,我发现了导致问题的原因:
My route defined in WebApiConfig.cswas like this:
我定义的路线WebApiConfig.cs是这样的:
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.
如您所见,它干扰了RouteConfig.cs.
回答by Raghu
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!
类名必须以Controller结尾!
Edit: As @Corey Alix has suggested, please make sure that the controller has a publicaccess modifier; non-public controllers are ignored by the route handler!
编辑:正如@Corey Alix 所建议的,请确保控制器具有公共访问修饰符;路由处理程序忽略非公共控制器!
回答by ElRaph
Another solution could be to set the controllers class permission to public.
另一种解决方案可能是将控制器类权限设置为公共。
set this:
设置这个:
class DocumentAPIController : ApiController
{
}
to:
到:
public class DocumentAPIController : ApiController
{
}
回答by Zapnologica
In my case I was using Web API and I did not have the publicdefined for my controller class.
就我而言,我使用的是 Web API,但没有public为我的控制器类定义。
Things to check for Web API:
检查 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}'
- 控制器类声明为
public - 控制器类实现 ApiController
: ApiController - 控制器类名需要以
Controller - 检查您的网址是否具有
/api/前缀。例如。'主机:端口/api/{控制器}/{actionMethod}'
回答by Sergey_T
In my case I wanted to create a Web API controller, but, because of inattention, my controller was inherited from Controller instead of ApiController.
就我而言,我想创建一个 Web API 控制器,但是由于疏忽,我的控制器是从 Controller 继承的,而不是 ApiController。
回答by Juliusz
In my case, the routing was defined as:
就我而言,路由定义为:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{*catchall}",
defaults: new { controller = "WarehouseController" }
while Controllerneeds to be dropped in the config:
而控制器需要在配置中删除:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{*catchall}",
defaults: new { controller = "Warehouse" }
回答by RobertH
In my case I was seeing this because I had two controllers with the same name:
就我而言,我之所以看到这一点,是因为我有两个同名的控制器:
One for handling Customer orders called CustomersControllerand the other for getting events also called CustomersController
一个用于处理调用的客户订单CustomersController,另一个用于获取事件也调用CustomersController
I had missed the duplication, I renamed the events one to CustomerEventsControllerand it worked perfectly
我错过了重复,我将事件重命名为一个CustomerEventsController,它运行良好
回答by Erkin Djindjiev
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.
遇到过这个类似的问题。我们正在处理多个 API,但我们遇到了错误的端口号并收到此错误。让我们永远意识到。确保您正在访问的 api 端口是正确的端口。
回答by Leonid Ershov
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.
面临同样的问题。在这里检查了所有答案,但我的问题出在命名空间上。System.Web.Mvc 和 System.Web.Http 中存在路由属性。我的使用包括 Mvc 命名空间,这就是原因。对于 webapi,你需要使用System.Net.Http。
回答by Gopi P
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.
我也遇到过同样的问题。搜索了很多,发现需要class级权限。默认情况下,类权限级别是内部的,所以我认为它不会影响程序执行。但它实际上受到了影响,您应该将您的班级许可设为公开,这样您就不会遇到任何问题。
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.
还有一个。如果是 webapi 项目,您的 webapirouteconfig 文件将覆盖 routeconfig.cs 文件设置。因此,请更新 webapi routeconfig 文件以使其正常工作。

