Url.Action based on current route

I would like to create a new URL based on an existing route, but will add a new 'page' parameter

Here are some examples:

old: ~ / localhost / something? what = 2
new: ~ / localhost / something? what = 2 & page = 5

old: ~ / localhost / Shoes
new: ~ / localhost / Shoes / 5

I cannot just add & page = 5 to an existing URL because the routes may be different.

Some use the query string, and some do not.

+6
asp.net-mvc
source share
3 answers

I had a similar problem and made an approach to the UrlHelper extension. The code in the view looks like this:

<a href="<%= Url.AddPage(2) %>">Page 2</a> 

The UrlHelper extension looks like this:

 using System.Web.Mvc; using System.Web.Routing; using System.Collections.Specialized; public static class UrlHelperExtension { public static string AddPage(this UrlHelper helper, int page) { var routeValueDict = new RouteValueDictionary { { "controller", helper.RequestContext.RouteData.Values["controller"] }, { "action" , helper.RequestContext.RouteData.Values["action"]} }; if (helper.RequestContext.RouteData.Values["id"] != null) { routeValueDict.Add("id", helper.RequestContext.RouteData.Values["id"]); } foreach (string name in helper.RequestContext.HttpContext.Request.QueryString) { routeValueDict.Add(name, helper.RequestContext.HttpContext.Request.QueryString[name]); } routeValueDict.Add("page", page); return helper.RouteUrl(routeValueDict); } } 

A few notes: I check the identifier, because I do not use it in all my routes. I am adding the page parameter value at the end, so this is the last url parameter (otherwise you could add it to the original constructor).

+6
source share

This seems like a good approach:

 // Clone Current RouteData var rdata = new RouteValueDictionary(Url.RequestContext.RouteData.Values); // Get QueryString NameValueCollection var qstring = Url.RequestContext.HttpContext.Request.QueryString; // Pull in QueryString Values foreach (var key in qstring.AllKeys) { if (rdata.ContainsKey(key)) { continue; } rdata[key] = qstring[key]; } // Update RouteData rdata["pageNo"] = "10"; // Build Url var url = Url.RouteUrl(rdata); 

and it avoids conflicts like: controller = example & action = problem, etc.

+1
source share

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.

0
source share

All Articles