Find Area name and Controller Name in custom Htmlhelper with ASP.NET MVC3

后端 未结 3 1125
情深已故
情深已故 2021-02-08 12:10

I try to rewrite and customize @Html.ActionLink, in one of overloads of this method the parameters are:

public static MvcHtmlString ActionLink(this         


        
3条回答
  •  借酒劲吻你
    2021-02-08 12:24

    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;
    
        ...
    }
    

提交回复
热议问题