Alex Rivera | Logout

How to get the current route ID within a View from the URL (ASP.NET MVC)

Asked 2009-06-11T05:46:49.260
79

Within the view that is returned from a URL such as /Controller/Action/1 (assuming the default route of controller/action/id), how can I get access to the ID from within the View?

I don't want to have to add it to the ViewData dictionary at the action level when handling the request.

Edit
Report

2 Answers

184

I think this is what you're looking for:

<%=Url.RequestContext.RouteData.Values["id"]%>
answered 2009-07-21T16:55:10.983
3

Adding it to the viewdata is the right thing to do. As for how to add it, you could always add a custom ActionFilter which grabs it from the route dictionary and pushes it into the viewdata.

answered 2009-06-11T14:43:53.287

Your Answer