views:

27

answers:

1

here are code I'm using to do, but compiler says: An anonymous type cannot have multiple properties with the same name

context.MapRoute("RouteName", "statics/category/{fileName}",
                            new
                            {
                                controller = "myController",
                                action = "Index"
                            },
                            new
                            {
                                fileName = new fnRouteConstraint(),
                                fileName = new AnotherRouteConstraint()
                            });
+1  A: 

The error is pretty straightforward: you're creating an anonymous class with two properties that have the same name. It'd be the same as writing:

public class m {
    public string p { get; set; }
    public string p  { get; set; }
}

To fix the problem, you'll have to create another IRouteConstraint that contains the logic from the two constraints you're trying to pass. Example: http://nayyeri.net/custom-route-constraint-in-asp-net-mvc

EDIT:

If you want to "merge" two separate route constraints, you just need to create a third constraint like this:

public ThirdRouteConstraint: IRouteConstraint {
    public ThirdRouteConstraint(){}

    public bool Match(HttpContextBase httpContext, Route route,
        string parameterName, RouteValueDictionary values,
        RouteDirection routeDirection) 
    {
        return (new FirstRouteConstraint().Match(httpContext, route, parameterName, values, routeDirection) &&
            new SecondRouteConstraint().Match(httpContext, route, parameterName, values, routeDirection));
    }

}
ewwwyn
yes, its pretty straightforward, i know, actually its an example. i want to know how i can use Multi-RouteConstraint. nayyeries doesn't described any approach that used two RC in one
Sadegh
good, because i am implemented IRouteConstraint as explicitly, i haven't access to Match method. thanks
Sadegh

related questions