tags:

views:

190

answers:

2

I'd like to generate a new URL based on the existing route, but will add a new parameter 'page'
Here are a few examples:

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

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

I can not just append &page=5 to existing url because routes may be different.
Some use the query string and some do not.

A: 

You could reconstruct a url by pulling out the parts of the existing route by way of the RouteData object. For instance, the following would render a url with the controller and action of the current route:

<%=Url.RouteUrl(new { controller = ViewContext.RouteData.Values["controller"], 
                      action = ViewContext.RouteData.Values["action"] }) %>

To get you started, you could go with something like a custom extension method that generates the url with an additional "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 construct a properly formatted link based on the routing configuration, whether page is real segment in the url or just appended as a querystring.

Kurt Schindler
The number of parameters that I have in the route is not constant.I may have 10 or may have only 1 (id in your example).
šljaker
I'll have to accept this since there are no other answers :)
šljaker
A: 

I had a similar issue, and took the approach of extending the UrlHelper. The code in the View looks like:

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

The UrlHelper extension looks like:

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 couple of notes: I check for the ID, since I don't use it in all my routes. I add the Page route value at the end, so it is the last url parameter (otherwise you could add it in the initial constructor).

jsalwen