ASP.NET Routing - load routes from database?

丶灬走出姿态 提交于 2019-12-07 18:06:59

问题


Is it possible to load routes from the database 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...


回答1:


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



来源:https://stackoverflow.com/questions/2433435/asp-net-routing-load-routes-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!