加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 找不到与名为’help’的控制器匹配的类型

发布时间:2020-12-16 09:25:33 所属栏目:asp.Net 来源:网络整理
导读:我一直在关注 this guide以添加帮助页面来记录我的Web API项目.我的控制器名为HelpController,我有一条路线,我试图将索引操作映射到/帮助.这是项目中唯一的MVC控制器.因为其余的是Web API控制器,我们从WebAPIConfig.cs中的默认路由中删除了“/ api”前缀. He
我一直在关注 this guide以添加帮助页面来记录我的Web API项目.我的控制器名为HelpController,我有一条路线,我试图将索引操作映射到/帮助.这是项目中唯一的MVC控制器.因为其余的是Web API控制器,我们从WebAPIConfig.cs中的默认路由中删除了“/ api”前缀.

HelpController:

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);
  // ..
}

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读