If I navigate to the following stackoverflow URL http://stackoverflow.com/questions/15532493
it is automatically appended with the title of the question like so:
https://mcmap.net/q/1168261/-mvc-custom-route-gives-404
That is, I can type the URL into my browser without the question title and it is appended automatically.
How would I go about achieving the same result in my application? (Note: I am aware that the question title doesn't affect the page that is rendered).
I have a controller called Users
with an action method called Details
. I have the following route defined:
routes.MapRoute("UserRoute",
"Users/{*domain}",
new { controller = "User", action = "Details" },
new { action = "^Details$" });
As this is an intranet application the user is authenticated against their Windows account. I want to append the domain and user name to the URL.
If I generate the URL in the view like so:
@Html.ActionLink("Model.UserName", "Details", "User", new { domain = Model.Identity.Replace("\\", "/") })
I get a URL that look like this:
Domain/Users/ACME/jsmith
However, if the user navigates to the URL Domain/Users/
by using the browsers navigation bar it matches the route and the user is taken to the user details page. I would like to append the ACME/jsmith/
onto the URL in this case.
The research I have done so far indicates I might have to implement a custom route object by deriving from RouteBase
and implementing the GetRouteData
and GetVirtualPath
methods but I do not know where to start with this (the msdn documentaiton is very thin).
So what I would like to know is:
- Is there a way of achieving this without implementing a custom route?
- If not, does anyone know of any good resources to get me started implementing a custom route?
- If a custom route implementation is required, how does it get at the information which presumably has to be loaded from the database? Is it OK to have a service make database calls in a route (which seems wrong to me) or can the information be passed to the route by the MVC framework?
how-to-generate-strings-like-this
which is not what I'm asking. I want to append the values to the url if the user enters just theDomain/Users/
bit in the browsers navigation bar. – Krucik