asp.netasp.net-mvcasp.net-web-apiroutes

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


I have been following this guide to add a help page to document my Web API project. My Controller is named HelpController and I have a route that I am trying to use to map the Index action to /Help. This is the only MVC controller in the project. Because the rest are Web API controllers, we removed the "/api" prefix from the default route in WebAPIConfig.cs.

The HelpController:

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

And route config:

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

    }
}

In Global.asax.cs

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

But when I try to navigate to /help in the browser I get the following error message.

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

EDIT: The message contains /ws/help as the application is hosted at localhost/ws in IIS.

Does anyone know what could be causing ASP.NET to not find my HelpController?

UPDATE: If I change the order of RouteConfig and WebApiConfig registration calls in Application_Start I get a 404 instead.

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

Solution

  • The request for help is being matched by Web API's route as you have removed api from its route template. if a request matches a route, further probing is not done on rest of the routes. You probably have the default order in Global.asax where Web API routes are registered first and then the MVC routes. Could you share how your Global.asax looks like?

    EDIT: Based on your last comment, if you install HelpPage nuget package, make sure that the order in your Global.asax looks like this:

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