Improvements to URL generation

May 3, 2011 at 2:34 AM

Hi,

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))
    {
        queryString.Remove(_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;
    else
        queryString[_pagerOptions.PageIndexParameterName] = pageIndex.ToString();

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

...Stefan

Sep 15, 2011 at 1:48 PM

and where is the UriParsingHelper class?