I have a project that can be accessed at the URL:
myapp.com/project/my-project
Route
To be able to use this route, I recorded the following in my Global.asax
routes.MapRoute(
"Project", // Route name
"Project/{url}/{action}", // URL with parameters
new { controller = "Project", action = "Details" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Action
When you browse the URL ´myapp.com/project/create´, I would like to ´create´ action is called, not my ´details´ action.
But the URL myapp.com/project/my-project should usually be interpreted by the details action.
public ActionResult Details(string projectUrl)
{
var project = _projectService.Get(projectUrl);
if (project == null)
//Not found! Redirect to next route!
var model = Mapper.Map<ProjectViewModel>(project);
return View(model);
}
Question
How to make, Create, Delete, Edit to be interpreted by the default route?
Here is the create route with your existing route:
Your project url parameter name should be
urlnotprojectUrlas that is the name of the route value.Here is my
ProjectController:The appropriate action is hit when I go to either of these URLs:
And pass “test” as the url value.
Its worth noting that routes are processed in the order they are added, so you need to make sure they go in order from most specific to most general like you have, and not reverse that.