HtmlHelperの拡張メソッドを作成しました(アクティブなメニュー項目-asp.net mvc3マスターページから派生)。これにより、現在のページのcssclass「アクティブ」を出力できます。
ただし、エリアを使用するようにリファクタリングしたため、ホームと呼ばれるコントローラーとインデックスと呼ばれるアクションがいくつかのエリアにあるため、このメソッドは機能しなくなりました。そのため、routevalues匿名タイプの一部として渡されたAreaを使用して現在のエリアをチェックすることにより、これを整理しようとしています。
したがって、私の拡張メソッドは次のようになります。
public static MvcHtmlString NavigationLink<T>(this HtmlHelper<T> htmlHelper, string linkText, string actionName, string controllerName, dynamic routeValues)
{
string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
string currentArea = htmlHelper.ViewContext.RouteData.DataTokens["Area"] as string;
if (controllerName == currentController && IsInCurrentArea(routeValues,currentArea))
{
return htmlHelper.ActionLink(
linkText,
actionName,
controllerName,
(object)routeValues,
new
{
@class = "active"
});
}
return htmlHelper.ActionLink(linkText, actionName, controllerName, (object)routeValues, null);
}
private static bool IsInCurrentArea(dynamic routeValues, string currentArea)
{
string area = routeValues.Area; //This line throws a RuntimeBinderException
return string.IsNullOrEmpty(currentArea) && (routeValues == null || area == currentArea);
}
次の行をコンパイルできるように、routeValuesのタイプを動的に変更しました。
文字列領域=routeValues.Area;
デバッガーのrouteValuesオブジェクトのAreaプロパティを確認できますが、アクセスするとすぐにRuntimeBinderExceptionが発生します。
匿名タイプのプロパティにアクセスするためのより良い方法はありますか?