ASP.Net WebForms路由多个目标的单个路由 [英] ASP.Net WebForms Routing Single Route for Multiple Destinations
问题描述
我正在考虑为我计划创建的新网站设置数据库路由。我一直在关注以下有关利用数据库中的friendlyUrl的教程:
I am looking into setting database routing up for a new website I plan to create. I have been looking at the following tutorial with regards to utilising friendlyUrls from a database:
但是,我想将相同的路由结构用于多个实体。含义:
However, I would like to use the same route structure for multiple entities. Meaning:
mysite.com/ {PlayerName}进入player.aspx
mysite.com/{TeamName}进入team.aspx
……等等……
mysite.com/{PlayerName} goes to player.aspx mysite.com/{TeamName} goes to team.aspx … and so on …
有人可以指出使用asp.net实现此目标的正确方向。可以使用内置的路由引擎,还是应该为此编写自己的HTTPModule?
Could somebody point in the right direction of achieving this with asp.net. Is it possible using the built in routing engine, or should I be looking to code my own HTTPModule for this?
感谢
David
Thanks David
推荐答案
写两个约束,它会返回布尔值,无论该段是否是团队/是否是球员/是否是球员。
Write two constraints which return boolean whether segment is a team or not / a player or not.
public class IsTeamConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
return SomeService.IsTeam(values["teamName"]);
}
}
public class IsPlayerConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
return SomeService.IsPlayer(values["playerName"]);
}
}
在页面路由中设置约束。
Set constraint in page route.
void RegisterCustomRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"Team",
"{teamName}",
"~/Team.aspx",
false,
null,
new RouteValueDictionary { { "isTeam", new IsTeamConstraint() } }
);
routes.MapPageRoute(
"Player",
"{playerName}",
"~/Player.aspx",
false,
null,
new RouteValueDictionary { { "isPlayer", new IsPlayerConstraint() } }
);
}
现在,当请求页面时,已注册的页面路由将使用约束条件来检查
Now when a page is requested registered page routes will use constraint to check that the route is valid and execute page if it is.
我没有在ASP.Net Forms中尝试过此操作,但是我运行的应用程序在ASP.Net MVC中开发了约束。两种类型的应用程序(窗体和MVC)都共享通用的路由逻辑。
I haven't tried this in ASP.Net Forms but I've applications running with constraints developed in ASP.Net MVC. Both type of application (Forms and MVC) shared common routing logic.
这篇关于ASP.Net WebForms路由多个目标的单个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!