简体   繁体   中英

Create object from List elements

I'm very sorry the title may not be informative enough, but I don't know how to explain what i'm trying to do in a question.

So, I have an MVC application, and i'm loading my routes from a configuration section in the Web.config .

In order to encapsulate all the information I need from the configured elements, I created a RouteModel.

IEnumerable<RouteModel> configuredRoutes = RoutingFacade.GetAllRoutes();

foreach(RouteModel route in configuredRoutes)
{
    routes.MapRoute(route.Name, route.Url,
                    new { controller = "Home", action = "Index", managers = route.Managers });
}

Ignore the managers key over there.

So far so good, but my problem is this.
My RouteModel has a Constraints property that returns a List<ConstraintModel> with all the configured constraints for that route.
ConstraintModel only has two properties, Name and Value .

As you may know, the MapRoute method takes an additional object parameter which are the constraints, this object is constructed like this:

new { constraint1 = value1, constraint2 = value2, .. }

How can I turn my List<ConstraintModel> into something like that?

I really appreciate you take your time to read my problem, thank you very much in advance

If you look at the methods of the RouteCollection class you will notice there is an Add method ( MSDN article here ).

What you can do is you can call it (that is what the MapRoute extension method does eventually) and create an instance of the Route class however you wish.

Dictionary<string, object> constraints = new Dictionary<string, object>();
// populate your constraints into the constraints dictionary here..

Dictionary<string, object> dataTokens = new Dictionary<string, object>();
Dictionary<string, object> defaults = new Dictionary<string, object>();

Route route = new Route(" << url >> ", new MvcRouteHandler())
{
    Constraints = new RouteValueDictionary(constraints),
    DataTokens = new RouteValueDictionary(),
    Defaults = new RouteValueDictionary()
};

routes.Add(route);

That way, you have the opportunity to specify string - object pairs for you constraints.

EDIT

Furthermore, if you have any doubts on how to use this method but are well versed in what the MapRoute extension method is concerned I suggest you use ILSpy to disassemble the assembly which defines MapRoute (which is System.Web.Mvc.dll ) and go step by step and discover the connection between MapRoute and RouteCollection.Add for yourself.

EDIT 2 - Concerning Route names

You can also check the overload RouteCollection.Add(string name, RouteBase item) . That is one way in which you could specify the name of the route.

So basically you could go and do this:

routes.Add(" My route with dynamically loaded constraints ", route);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM