Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ASP.NET MVC3:如何在HtmlHelper扩展方法中访问作为routeValues匿名类型传递的参数?_Asp.net Mvc 3_C# 4.0_Dynamic_Anonymous Types_Asp.net Mvc 3 Areas - Fatal编程技术网

Asp.net mvc 3 ASP.NET MVC3:如何在HtmlHelper扩展方法中访问作为routeValues匿名类型传递的参数?

Asp.net mvc 3 ASP.NET MVC3:如何在HtmlHelper扩展方法中访问作为routeValues匿名类型传递的参数?,asp.net-mvc-3,c#-4.0,dynamic,anonymous-types,asp.net-mvc-3-areas,Asp.net Mvc 3,C# 4.0,Dynamic,Anonymous Types,Asp.net Mvc 3 Areas,我已经为HtmlHelper(派生自)编写了一个扩展方法。这允许我为当前页面输出cssclass“active” 然而,我现在已经进行了重构以使用区域,因此该方法不再有效,因为我在几个区域中有名为Home的控制器和名为Index的操作。所以我一直试图通过检查当前区域和作为routevalues匿名类型的一部分传递的区域来解决这个问题 因此,我的扩展方法现在如下所示: public static MvcHtmlString NavigationLink<T>(this HtmlHelp

我已经为HtmlHelper(派生自)编写了一个扩展方法。这允许我为当前页面输出cssclass“active”

然而,我现在已经进行了重构以使用区域,因此该方法不再有效,因为我在几个区域中有名为Home的控制器和名为Index的操作。所以我一直试图通过检查当前区域和作为routevalues匿名类型的一部分传递的区域来解决这个问题

因此,我的扩展方法现在如下所示:

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);
}
public static MvcHtmlString NavigationLink(此HtmlHelper HtmlHelper、string linkText、string actionName、string controllerName、dynamic RouteValue)
{
string currentController=htmlHelper.ViewContext.RoutedData.GetRequiredString(“控制器”);
string currentArea=htmlHelper.ViewContext.RoutedData.DataTokens[“Area”]作为字符串;
if(controllerName==currentController&&IsInCurrentArea(routeValues,currentArea))
{
返回htmlHelper.ActionLink(
链接文本,
actionName,
控制器名称,
(对象)路由值,
新的
{
@class=“活动”
});
}
返回htmlHelper.ActionLink(linkText,actionName,controllerName,(object)RouteValue,null);
}
私有静态布尔值为当前区域(动态路由值、字符串当前区域)
{
string area=routeValues.area;//此行抛出RuntimeBinderException
返回字符串.IsNullOrEmpty(currentArea)&&(RouteValue==null | | area==currentArea);
}
我将RouteValue的类型更改为dynamic,以便可以编译以下行:

字符串区域=routeValues.area

我可以在调试器中看到routeValues对象上的Area属性,但一旦访问它,就会得到RuntimeBinderException


有没有更好的方法来访问匿名类型的属性?

我发现我可以使用RouteValueDictionary上的构造函数,它允许我轻松地查找
区域
属性

我还注意到,我还试图使用控制器值,这使问题变得复杂,因此我的代码现在看起来如下所示:

    public static MvcHtmlString NavigationLink<T>(this HtmlHelper<T> htmlHelper, string linkText, string actionName, string controllerName, object routeValues)
    {
        string currentArea = htmlHelper.ViewContext.RouteData.DataTokens["Area"] as string;

        if (IsInCurrentArea(routeValues, currentArea))
        {
            return htmlHelper.ActionLink(
                linkText,
                actionName,
                controllerName,
                routeValues,
                new
                {
                    @class = "active"
                });
        }
        return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, null);
    }

    private static bool IsInCurrentArea(object routeValues, string currentArea)
    {
        if (routeValues == null)
            return true;

        var rvd = new RouteValueDictionary(routeValues);
        string area = rvd["Area"] as string ?? rvd["area"] as string;
        return area == currentArea;
    }
public static MvcHtmlString导航链接(此HtmlHelper、字符串链接文本、字符串操作名、字符串控制器名、对象路由值)
{
string currentArea=htmlHelper.ViewContext.RoutedData.DataTokens[“Area”]作为字符串;
if(IsInCurrentArea(路由值、当前区域))
{
返回htmlHelper.ActionLink(
链接文本,
actionName,
控制器名称,
路线价值,
新的
{
@class=“活动”
});
}
返回htmlHelper.ActionLink(linkText、actionName、controllerName、RouteValue、null);
}
私有静态bool-IsInCurrentArea(对象RouteValue、字符串currentArea)
{
if(routeValues==null)
返回true;
var rvd=新的RouteValueDictionary(routeValues);
字符串区域=rvd[“区域”]作为字符串??rvd[“区域”]作为字符串;
返回区域==当前区域;
}