Jan 19, 2010

Links in asp.net

There are serveral helper class which help to generate links.

//When you generate link with the same controller 
Html.ActionLink();

//When you generate link with the different controller
Html.RouteLink();


Ajax.ActionLink(); //ajax version of Html.ActionLink(..)
Ajax.RouteLink();  //ajax version of Html.RouteLink(..)

//generate url only, not anchor tag
Url.RouteUrl();


//eventually all method will call
internal static string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) {
    RouteValueDictionary mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, requestContext.RouteData.Values, routeValues, includeImplicitMvcValues);

    VirtualPathData vpd = routeCollection.GetVirtualPath(requestContext, routeName, mergedRouteValues);
    if (vpd == null) {
        return null;
    }

    string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
    return modifiedUrl;
}