开发者

ASP.NET Routing - load routes from database?

开发者 https://www.devze.com 2022-12-22 14:37 出处:网络
Is it possible to load routes from the databa开发者_如何学Cse with ASP.NET ? For each r as SomeRouteObject in RouteDataTable

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号