I’m looking for the way to shorten the URL for getting some from DB. I’ve got Homecontroller and writing action, which adds rows to DB and each of them has UID. There is also search action which finds the record in DB anв renders a view.
So I use localhost/Home/writing to add a record and localhost/Home/search/3456 to search a record by UID.
Is there any way to pass UID (3456) into controller’s action skipping action’s name, so to search a resord I can use localhost/Home/3456 or even localhost/3456?
I’ve tried
routes.MapRoute(
"default-action",
"{controller}/{id}",
new { action = "search", id = UrlParameter.Optional }
);
and
routes.MapRoute(
"default-action",
"{id}",
new {controller = "Home", action = "search", id = UrlParameter.Optional }
);
but got nothing.
You could try using a route constraint:
Now:
localhost/Home/writingwill hit theWritingaction onHomeControllerlocalhost/3456will hit theSearchaction onHomeControllerand pass itid=3456as parameterThe numeric constrained we defined for the
{id}route token is required in order for the routing engine to disambiguate between the search and the default route patterns (since action names cannot start with a number). It assumes that your identifiers are numbers.