Say I have the following in my route.config
public static void RegisterRoutes(RouteCollection routes)
{
if (routes != null)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(name: "category", url: "{category}", defaults: new { controller = "Category", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
How do I do the equivalent of my category route using a routing attributes?
I have tried the following:
[Route("{action=Index}")]
public class CategoryController : Controller
{
[Route("{category}")]
public ActionResult Index(string category)
{
return View(category);
}
}
But this the throws up an error:
Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.
Is it possible or do I need to leave this one in the route.config?
/somewhere
), your routing is most likely misconfigured to begin with. For example/Home
won't access yourHomeController
andIndex
method, it will instead go toCategoryController
andIndex
method. Please specify exactly what URLs should go where in your question, because it is currently unclear. – NightOwl888