Improvements to URL generation

May 3, 2011 at 1:34 AM


I discovered that the URL being generated is not always correct as MVC routing doesn't always know how to generate the URL correctly.  The solution that I have implemented is to read the query strings and insert the page into the query string as seen in the code below. I am happy to provide this code and the associated UriParsingHelper class.

private string GenerateUrl(int pageIndex)
    //return null if  page index larger than total page count or page index is current page index
    if (pageIndex > _totalPageCount || pageIndex == _pageIndex)
        return null;

    // add the page query string
    string baseUri = UriParsingHelper.InternalUriGetBaseValue(new Uri(_html.ViewContext.HttpContext.Request.RawUrl, UriKind.RelativeOrAbsolute));
    IDictionary<string, string> queryString = UriParsingHelper.InternalUriParseQueryStringToDictionary(_html.ViewContext.HttpContext.Request.Url);
    if (queryString.ContainsKey(_pagerOptions.PageIndexParameterName))
    // set route value of page index parameter name in url,pageIndex==0 is a special case
    if (pageIndex == 0)
        queryString[_pagerOptions.PageIndexParameterName] = ScriptPageIndexName;
        queryString[_pagerOptions.PageIndexParameterName] = pageIndex.ToString();

    return UriParsingHelper.InternalUriCreateWithQueryStringValues(baseUri, queryString, "").ToString();


Sep 15, 2011 at 12:48 PM

and where is the UriParsingHelper class?