You can retrieve the URL by pulling portions of an existing route using the RouteData object. For example, the following will display the URL of the controller and the actions of the current route:
<%=Url.RouteUrl(new { controller = ViewContext.RouteData.Values["controller"], action = ViewContext.RouteData.Values["action"] }) %>
To get started, you can use something like a custom extension method that generates a URL with the optional page parameter. Adjust as necessary:
public static string UrlWithPage(this UrlHelper urlHelper, string name, int page) { string url = urlHelper.RouteUrl( new { controller = urlHelper.RequestContext.RouteData.Values["controller"], action = urlHelper.RequestContext.RouteData.Values["action"], id = urlHelper.RequestContext.RouteData.Values["id"], page = page } ); return "<a href=\"" + url + "\">" + name + "</a>"; }
This will build a properly formatted link based on the routing configuration, regardless of whether the page is a real segment in the URL or simply added as a request.
Kurt schindler
source share