I have a controller called Entry that has three ActionResults: View, New, and Edit.
The View action accepts a string parameter representing a specific Entry object.
I’m trying to figure out how to not show the word “View” in the URL. In other words, I’d like it to act like a default action.
Ideally I would like the URLs read as:
- /entry/2DxyJR for a given entry
- /entry/new to create a new entry
- /entry/edit/2DxyJR to edit a given entry
I believe this can be accomplished with a custom route but am unsure how to actually do it. This route works for hiding “View”, however /new and /edit don’t work.
routes.MapRoute(
name: "Entry",
url: "entry/{id}",
defaults: new { controller = "Entry", action = "View", id = UrlParameter.Optional }
);
Sorry for the extreme noobishness of this, but I’m still trying to wrap my head around how routing works.
You’ll need to be sure you put the more specific ones on top, so the entry/id will have to be last, because it appears you have string based id’s.
This will match the most explicit (new) first, then if there’s edit in the url, then if not fall through to the view action.