ASP.NET Routing - load routes from database?
Is it possible to load routes from the databa开发者_如何学Cse with ASP.NET ?
For each r as SomeRouteObject in RouteDataTable
routes.MapRoute( _
r.Name, _
r.RouteUri, _
r.RouteValues, _ //??
r.Constraints _ //??
)
Next
How should I store the routevalues / constraints? I understand that there are several 'default' routevalues like .Controller
and .Action
, however I also need entirely custom ones like .Id
or .Page
...
Think before you jump
It is possible but is not feasible. They've provided route configuration to be static in Global.asax, because routes tend to be static and stay the same for a long time. If they don't, you'll have SEO problems anyway. So I'd advise you to first stop and actually think if you really need configurable routes.
But it's possible
But it's possible to use DB in the background to have your routes defined. In your case you could store values as serialized string key/value pairs, because RouteValuesDictionary
can accept key-value pairs as well to feed values in.
If your route definitions stay the same in terms of defaults/constraints, but not URLs, you could provide your own interface and provide classes that implement it. In this case you could just store URLs and class definitions (similar as they are defined in web.config
) that define values for a particular route. This way it will make it very simple to use complex object configurations if you need them...
精彩评论