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
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 }
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With