ASPNET MVC The user route fails with the error: "Type not found, which corresponds to the controller named" Home ".

I am trying to create a custom route to handle a specific URL with several parameters. I added a new route definition to global.asax, but when I try to go to action, I get the error message "A type was not found that matches the controller named" Home ".

Can someone 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 click will look like http://localhost/home/detail/1234/1.2

+6
source share
2 answers

MapHttpRoute routes to the web API controller that inherits ApiController .

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

 routes.MapRoute( name: "HomeDetails", url: "Home/Detail/{articleId}/{articleVersion}", defaults: new { controller = "Home", action = "Detail", articleId = 0, articleVersion = 0.0 } ); 
+21
source

Hi, change the code this way.

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

Source: https://habr.com/ru/post/917165/


All Articles