ASP.NET 4.0 Web 表单路由
我有一个现有网站,我想将其转换为使用路由,并在阅读 Scott Guthrie 的帖子在这里,我构建了一个适用于大多数情况下。但是,由于并非现有站点上的所有页面都与特定模式匹配,因此我需要检查数据库以确定要使用哪个路由(目标 .aspx 页面)。
例如,大多数页面都是这样的:
http://www.mysite.com/people/person .html
这很好 - 由于“people”目录,我可以轻松地将它们路由到 view_person.aspx 页面。
但有些页面是这样的:
http://www.mysite.com/category_page.html http://www.mysite.com/product_page.html
这需要检查数据库以查看是否路由到 view_category.aspx 页面或 view_product.aspx 页面。 这就是我陷入困境的地方。我是否创建一个 IRouteHandler 来检查数据库并返回路由?或者有更好的方法吗?我发现的唯一适合的代码是 这个问题的答案。
提前致谢。
I have an existing site that I'd like to convert to use routing, and after reading Scott Guthrie's post here, I built a working sample that works for most circumstances. However, since not all of the pages on the existing site match a particular pattern, I'll need to check against a database to determine which route (destination .aspx page) to use.
For example, most pages are like this:
http://www.mysite.com/people/person.html
This is fine - I can easily route these to the view_person.aspx page because of the 'people' directory.
But some pages are like this:
http://www.mysite.com/category_page.html
http://www.mysite.com/product_page.html
This necessitates checking the database to see whether to route to the view_category.aspx page or the view_product.aspx page. And this is where I'm stuck. Do I create an IRouteHandler that checks the database and returns the route? Or is there a better way? The only code I've found that kind of fits is the answer to this question.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不介意这样做,最干净的解决方案是:
在 ASP.NET MVC 中,通过在根路由上指定默认控制器和操作方法,处理这种情况的方式略有不同。
If you don't mind doing so, the cleanest solution is to:
In ASP.NET MVC, this situation would be handled a little differently, by specifying a default controller and action method on the root route.
您的路线处理程序不会检查数据库。它将所有请求发送到处理程序 .aspx 脚本。这是检查数据库的脚本。
我的注册路由看起来像...
自定义处理程序(在 ASP.Net 4.0 中不需要)只是将 urlname 参数作为 URL 变量传递给响应脚本。
现在,响应脚本检查数据库的频率取决于数据库中数据更改的频率。例如,您可以轻松地缓存路径并在数据发生更改时使缓存失效。
Your route handler doesn't check the database. It sends all the requests to a handler .aspx script. It's that script that checks the database.
My register route looks like...
The custom handler, which shouldn't be needed in ASP.Net 4.0, simply passes the urlname parameter to the responding script as a URL variable.
Now how often the responding script checks the database depends on how often the data in the database is changed. You can easily cache paths and invalidate the cache when the data is suppose to have changed for instance.
对于陷入同样情况的任何人,我最终调整了 此答案用于检查数据库并返回正确的 ASPX 页面。
For anyone stuck in the same situation, I ended up adapting the code from this answer to check against a database and return the proper ASPX page.