The Actionlink implementation inside System.Web.Mvc.Html.LinkExtensions (System.Web.Mvc, Version=4.0.0.0)
is as below...
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
if (string.IsNullOrEmpty(linkText))
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
else
return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string) null, actionName, controllerName, routeValues, htmlAttributes));
}
There are a number of utility overloads with different parameter combinations.
So you should be able to add an extension method to generate your link easily (untested)...
public static MvcHtmlString InternalActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
if (string.IsNullOrEmpty(linkText))
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
else
RouteValueDictionary.Values.Add("id", htmlHelper.ViewContext.RequestContext.RouteData)
return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string) null, actionName, controllerName, routeValues, htmlAttributes));
}
public static MvcHtmlString InternalActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)
{
return this.InternalActionLink(htmlHelper, linkText, actionName, (string) null, new RouteValueDictionary(), (IDictionary<string, object>) new RouteValueDictionary());
}
Obviously you'd need to add some simple wrapper overloads to provide the exact functionality you want.
and then just use html.InternalActionLink("Title", "Action")
and it will automatically inject the correct Id into the route dictionary when generating the Url.
Incidentally, the following ActionLink()
overloads are provided by the framework - the more you implement, the more flexible you'll be...
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)