You can get the current controller and action from htmlHelper.ViewContext.RouteData . Use the extension method below to get the corresponding value from xml:
//make sure you include System.Xml.XPath, otherwise extension methods for XPath //won't be available using System.Xml.XPath; public static MvcHtmlString Show(this HtmlHelper htmlHelper, string key) { XElement element = XElement.Load("path/to/yourXmlfile.xml"); RouteData routeData = htmlHelper.ViewContext.RouteData; var keyElement = element.XPathSelectElements(string.format("//{0}/{1}/{2}", routeData.GetRequiredString("controller"), routeData.GetRequiredString("action"), key) ).FirstOrDefault(); if (keyElement == null) throw new ApplicationException( string.format("key: {0} is not defined in xml file", key)); return new MvcHtmlString(keyElement.Value); }
Abdul Munim
source share