Reputation: 23
Wondering if I can get some opinions on how to handle routing for a web application I'm currently working on. Since it's a fairly complex app with many areas the default routing isn't quite cutting it. Here's the setup:
URL I'd like to have - /management/assets/course/edit/id
Currently, we have an area for management.
I'm having trouble understanding how to make /management/{controller}/{action}/{id}
into /management/assets/{controller}/{action}/{id}
If there's better practices please let me know. I'm also looking into the new attribute routing in MVC5 as a solution.
Upvotes: 1
Views: 82
Reputation: 1809
AttributeRouting should be the way to go about it:
[RouteArea("management")]
[RoutePrefix("assets/course")]
public class AssetsCourseController : Controller
{
[Route("edit/{id}")]
public ActionResult Edit(string id) { ... }
}
This allows you much more control on your routes and simplifies custom routing greatly when you have deep hierarchies of routes.
To enable it, change this in RouteConfig.cs
:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Setup attributing routing rules
routes.MapMvcAttributeRoutes();
// You can comment out the old routing if you don't need it,
// but it can work in hybrid mode without issues
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
You could leave in the old routing and have hybrid routes and only override with AttributeRouting when needed if you already have a working site and needs to keep it working.
Upvotes: 1