さらに簡単:
htmlHelper.ViewContext.RouteData.Values["controller"]
と
htmlHelper.ViewContext.RouteData.Values["action"]
ができます。ここ
//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);
}