我的问题是关于 ASP.net (VB) Web 表单网站中的页面路由。
我需要以多种方式路由到 2 个 .aspx 页面,例如,
routes.MapPageRoute("SEO", "{Title}/{Id}", "~/PageA.aspx")
routes.MapPageRoute("Catalogue", "Issue{IssueNumber}-{PageNumber}", "~/PageA.aspx")
但我需要在两条路由上实现一些涉及数据库查询(LINQ to SQL)的逻辑,例如
路由 1)检查位字段,如果为 false,则物理文件 = PageA.aspx, true then PageB.aspx
Route 2) 查找 IssueNumber 和 PageNumber,检索 PageId 并添加到 RouteData,设置物理文件 = PageA.aspx
我认为最好的方法是实现一个 IRouteHandler 类,但是我无法确定:
- 在此类中的何处编写数据库查询
- 如何在类中设置物理文件 在
- 何处/如何向路由数据(即 PageId)添加新值 在
- 何处检查 Id 和 Number 字段实际上是整数(约束?)
我找不到任何有用的 VB.net 文档,有什么建议吗?
如果不是,我将不得不求助于中间 .aspx 页面,即 Transfer.aspx,然后执行数据库查询,然后将返回值存储在会话变量中,然后执行 Server.Transfer(PageA.aspx)< /code>,但这似乎是一种老式且不优雅的做法。请帮忙!
My question is regarding Page Routing in an ASP.net (VB) Web Forms website.
I need to route to 2 .aspx pages in multiple ways e.g.
routes.MapPageRoute("SEO", "{Title}/{Id}", "~/PageA.aspx")
routes.MapPageRoute("Catalogue", "Issue{IssueNumber}-{PageNumber}", "~/PageA.aspx")
but I need to implement some logic involving database queries (LINQ to SQL) on both routes e.g.
Route 1) Check a bit field, if false then physical file = PageA.aspx, true then PageB.aspx
Route 2) Lookup IssueNumber and PageNumber, retrieve PageId and add to RouteData, set physical file = PageA.aspx
I think the best way of doing this, is to implement an IRouteHandler class but I've not been able to determine:
- Where to write the database queries in such class
- How to set the physical file in the class
- Where/how to add a new value to the route data i.e. PageId
- Where to check that Id and Number fields are actually integers (constraints?)
I can't find any useful VB.net documentation, any suggestions?
If not I'm going to have to resort to an intermediate .aspx page i.e. Transfer.aspx and then do the database queries and then store return values in session variables and then do a Server.Transfer(PageA.aspx)
, but this seems like an old-fashioned and inelegant way of doing it. Please help!
发布评论
评论(1)
我建议不要编写自己的 IRouteHandler,而是实现您的 Route 类。重写 GetRouteData 并根据需要设置返回的 RouteData 对象。
如上所述,GetRouteData 就是您要寻找的地方。
在您返回的 RouteData 对象上,将 RouteHandler 设置为新的 PageRouteHandler 实例。您可以将物理路径传递给 PageRouteHandler 的构造函数。
使用 RouteData 对象的 Values 属性。
这应该通过路由约束来完成。例如,MapPageRoute 的第六个参数是带有约束的 RouteValueDictionary。要简单地检查参数是否为整数,请使用正则表达式,如下所示:
看到末尾的“\d+”了吗?这是id参数需要匹配的正则表达式。
如果您需要更复杂的约束,也可以这样做,请参阅 http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
Instead of writing your own IRouteHandler I'd suggest implementing your Route class. Override GetRouteData and setup the RouteData object that you return according to your needs.
As mentioned above, GetRouteData is the place you are looking for.
On the RouteData object you return, set RouteHandler to a new PageRouteHandler instance. You can pass the physical path to PageRouteHandler's constructor.
Use the Values property of the RouteData object.
This should be done with route constraints. The sixth parameter to MapPageRoute for example is a RouteValueDictionary with contraints. To simple check that a parameter is an integer, use a regular expression, like so:
See the "\d+" at the end? This is the regular expression that the id parameter needs to match.
If you need more complex constraints you can do that as well, see e.g. http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx