tags:

views:

43

answers:

2

I am adding some functionality to the HtmlHelper-class. Basically I want to automatically disable links on a web page based on user privileges e t c.

So I have this function:

public static string ActionLinkWithPrivileges(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
    {
        return LinkExtensions.ActionLink(htmlHelper, linkText, actionName, routeValues);
    }

The problem here is the routeValues-argument. Its usually created as an anonymous type so I dont know what to cast it to. This anonymous type often has a property named "id" but just writing routeValue.id gives me a compiler error.

Any help would be appreciated!

A: 

either implement an interface or use reflection to get the PropertyInfo and then itterate through the property collection to get the right one.

you would of course need to tell the method the name of the property to get unless it's of a particular type.

griegs
A: 

This should work :

RouteValueDictionary routeVals = new RouteValueDictionary(routeValues);
var value = routeVals["key"];
//RouteValueDictionary is under System.Web.Routing
çağdaş