How to prevent Url.RouteUrl(…) from inheriting route values from the current request

My solution was this :

An HtmlExtension helper method :

    public static string RouteUrl(this UrlHelper urlHelper, string routeName, object routeValues, bool inheritRouteParams)
    {
        if (inheritRouteParams)
        {
            // call standard method
            return urlHelper.RouteUrl(routeName, routeValues);
        }
        else
        {
            // replace urlhelper with a new one that has no inherited route data
            urlHelper = new UrlHelper(new RequestContext(urlHelper.RequestContext.HttpContext, new RouteData()));
            return urlHelper.RouteUrl(routeName, routeValues);
        }
    }

I can now do :

Url.RouteUrl('testimonials-route', new { }, false)

and know for sure it will always behave the same way no matter what the context.

The way it works is to take the existing UrlHelper and create a new one with blank ‘RouteData’. This means there is nothing to inherit from (even a null value).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)