I try to rewrite and customize @Html.ActionLink
, in one of overloads of this method the parameters are:
public static MvcHtmlString ActionLink(this
Use this:
string controllerName =
(string)htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
string areaName =
(string)htmlHelper.ViewContext.RouteData.DataTokens["area"];
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName
)
{
RouteData rd = htmlHelper.ViewContext.RouteData;
string currentController = rd.GetRequiredString("controller");
string currentAction = rd.GetRequiredString("action");
// the area is an optional value and it won't be present
// if the current request is not inside an area =>
// you need to check if it is null or empty before using it
string area = rd.Values["area"] as string;
...
}
I believe "controller" and "area" should be lower case. Here is how to get the area value:
ASP.NET MVC - Get Current Area Name in View or Controller
If not currently in an area it would give a object reference exception, so check for null first and then set the value if it is not null. Your controller is also correct, just try it in lower case. Hope this helps