【问题标题】:No type was found that matches the controller named 'help'找不到与名为“帮助”的控制器匹配的类型
【发布时间】:2013-12-21 03:25:59
【问题描述】:

我一直关注this guide 添加帮助页面来记录我的 Web API 项目。我的控制器被命名为 HelpController,我有一个路由,我试图用它来将 Index 操作映射到 /Help。这是项目中唯一的 MVC 控制器。因为其余的是 Web API 控制器,所以我们从 WebAPIConfig.cs 中的默认路由中删除了“/api”前缀。

帮助控制器:

public class HelpController : Controller
{
    public ActionResult Index()
    {
        var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
        return View(apiExplorer);
    }
}

和路由配置:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "help",
            defaults: new { controller = "Help", action = "Index"});

    }
}

在 Global.asax.cs 中

protected void Application_Start()
{
  // ..
  WebApiConfig.Register(GlobalConfiguration.Configuration);
  RouteConfig.RegisterRoutes(RouteTable.Routes);
  // ..
}

但是当我尝试在浏览器中导航到 /help 时,我收到以下错误消息。

<Error>
  <Message>No HTTP resource was found that matches the request URI 'http://localhost/ws/help'.</Message>
  <MessageDetail>No type was found that matches the controller named 'help'.</MessageDetail>
</Error>

编辑:该消息包含 /ws/help,因为应用程序托管在 IIS 中的 localhost/ws 上。

有谁知道是什么原因导致 ASP.NET 找不到我的 HelpController?

更新:如果我在 Application_Start 中更改 RouteConfig 和 WebApiConfig 注册调用的顺序,我会得到 404。

protected void Application_Start()
{
  // ..
  RouteConfig.RegisterRoutes(RouteTable.Routes);
  WebApiConfig.Register(GlobalConfiguration.Configuration);
  // ..
}

【问题讨论】:

  • 这条路线似乎适用于http://localhost/help,您似乎要去http://localhost/ws/help,对吗?
  • 对不起,是的。这在 IIS 的网站中托管为一个名为 ws 的应用程序。
  • 错误信息指的是help而不是Help(注意大小写)你是否使用Help作为url进行了测试。
  • @TomStyles 是的。不幸的是,同样的反应。

标签: asp.net asp.net-mvc asp.net-web-api routing


【解决方案1】:

help 的请求正在与 Web API 的路由匹配,因为您已从其路由模板中删除了 api。如果请求与路由匹配,则不会对其余路由进行进一步探测。 您可能在 Global.asax 中有默认顺序,其中首先注册 Web API 路由,然后是 MVC 路由。您能分享一下您的 Global.asax 的样子吗?

编辑: 根据您上次的评论,如果您安装 HelpPage nuget 包,请确保 Global.asax 中的顺序如下所示:

AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);

【讨论】:

  • 我没有将它安装为区域,只是按照我链接的教程进行的,没有提到任何地方的区域。
  • 嗯...你不是从nuget包Microsoft.AspNet.WebApi.HelpPage安装HelpPage吗?...我问是因为当你这样做时,这个帮助页面会安装在它自己的HelpPage区域...有你已经看过这个包了吗?...
  • 如果您在回答中添加使用 Nuget 包的建议,我会将其标记为已接受。不太有效,但问题与这个问题不同。
【解决方案2】:

我今天遇到了同样的错误。

<Error>
  <Message>No HTTP resource was found that matches the request URI 'xxx'.</Message>
  <MessageDetail>No type was found that matches the controller named 'xxx'.</MessageDetail>
</Error>

在我的情况下,经过 2 天的调试,我终于通过将 microsoft 报告查看器 dll 设置为“复制到本地”来解决它。对我来说它是如何相关的毫无意义,但这可能会对某人有所帮助。

【讨论】:

    【解决方案3】:

    我遇到了同样的错误:

    Error><Message>No HTTP resource was found that matches the request URI 'http://localhost:53569/api/values'.</Message><MessageDetail>No type was found that matches the controller named 'values'.</MessageDetail></Error>
    

    默认情况下,当我在 asp.net 网络表单应用程序中创建新控制器时,它是这样的:

    ValuesController1 : ApiController
    

    我只是简单地删除了“1”,然后让它:

    ValuesController : ApiController
    

    而且有效,不知道是bug还是什么,但是给我带来了很大的麻烦。

    【讨论】:

    • 在 url 中写入 xyz 的控制器,匹配名为 xyzController 的控制器类,这就是 '1' 出现问题的原因
    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多