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 

//When you generate link with the different controller

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

//generate url only, not anchor tag

//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;