Get Current Area Name in View or Controller

前端 未结 12 1584
闹比i
闹比i 2020-11-29 00:50

How do you get the current area name in the view or controller?

Is there anything like ViewContext.RouteData.Values[\"controller\"] for areas?

12条回答
  •  半阙折子戏
    2020-11-29 01:18

    I just wrote a blog entry about this, you can visit that for more details, but my answer was to create an Extension Method, shown below.

    The key kicker was that you pull the MVC Area from the .DataTokens and the controller/action from the .Values of the RouteData.

    public static MvcHtmlString TopMenuLink(this HtmlHelper htmlHelper, string linkText, string controller, string action, string area, string anchorTitle)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
            var url = urlHelper.Action(action, controller, new { @area = area });
    
            var anchor = new TagBuilder("a");
            anchor.InnerHtml = HttpUtility.HtmlEncode(linkText);
            anchor.MergeAttribute("href", url);
            anchor.Attributes.Add("title", anchorTitle);
    
            var listItem = new TagBuilder("li");
            listItem.InnerHtml = anchor.ToString(TagRenderMode.Normal);
    
            if (CheckForActiveItem(htmlHelper, controller, action, area))
                listItem.GenerateId("menu_active");
    
            return MvcHtmlString.Create(listItem.ToString(TagRenderMode.Normal));
        }
    
        private static bool CheckForActiveItem(HtmlHelper htmlHelper, string controller, string action, string area)
        {
            if (!CheckIfTokenMatches(htmlHelper, area, "area"))
                return false;
    
            if (!CheckIfValueMatches(htmlHelper, controller, "controller"))
                return false;
    
            return CheckIfValueMatches(htmlHelper, action, "action");
        }
    
        private static bool CheckIfValueMatches(HtmlHelper htmlHelper, string item, string dataToken)
        {
            var routeData = (string)htmlHelper.ViewContext.RouteData.Values[dataToken];
    
            if (routeData == null) return string.IsNullOrEmpty(item);
    
            return routeData == item;
        }
    
        private static bool CheckIfTokenMatches(HtmlHelper htmlHelper, string item, string dataToken)
        {
            var routeData = (string)htmlHelper.ViewContext.RouteData.DataTokens[dataToken];
    
            if (dataToken == "action" && item == "Index" && string.IsNullOrEmpty(routeData))
                return true;
    
            if (dataToken == "controller" && item == "Home" && string.IsNullOrEmpty(routeData))
                return true;
    
            if (routeData == null) return string.IsNullOrEmpty(item);
    
            return routeData == item;
        }
    

    Then you can implement it as below :

    
    

提交回复
热议问题