Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPNET MVC Custom route fails with "No type was found that matches the controller named 'Home'."

I'm trying to create a custom route to handle a specific url with multiple parameters. I have added a new new route definition to the global.asax but when I try to navigate to the action I get the error message "No type was found that matches the controller named 'Home'."

Can anyone tell me where I am going wrong?

Global.asax:

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

  routes.MapHttpRoute(
      name: "HomeDetails",
      routeTemplate: "Home/Detail/{articleId}/{articleVersion}",
      defaults: new { controller = "Home", action = "Detail", articleId = "0", articleVersion = "0.0" }
  );

  //routes.MapHttpRoute(
  //    name: "DefaultApi",
  //    routeTemplate: "api/{controller}/{id}",
  //    defaults: new { id = RouteParameter.Optional }
  //);

  routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );
}

Controller:

public class HomeController : Controller
{

  public ViewResult Index()
  {
    return View();
  }

  public ViewResult Detail(int articleId, decimal articleVersion)
  {
  // does stuff here...
  return View(model);
  }

}

The url I'm trying to hit would be something like http://localhost/home/detail/1234/1.2

like image 746
Nick Avatar asked Jun 01 '12 09:06

Nick


1 Answers

MapHttpRoute routes to a Web API controller inheriting ApiController.

In your example HomeController inherits the standard Controller (not ApiController), you need to use MapRoute instead for the controller to be found:

routes.MapRoute(
  name: "HomeDetails",
  url: "Home/Detail/{articleId}/{articleVersion}",
  defaults: new { controller = "Home", action = "Detail", articleId = 0, articleVersion = 0.0 }
);
like image 50
pjumble Avatar answered Sep 27 '22 17:09

pjumble