ASP.Net WebForms Routing Single Route for Multiple Destinations(ASP.Net WebForms 路由多个目的地的单一路由)
问题描述
我正在考虑为我计划创建的新网站设置数据库路由.我一直在查看以下有关使用数据库中的friendlyUrls的教程:
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:
http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/url-routing
但是,我想为多个实体使用相同的路由结构.含义:
However, I would like to use the same route structure for multiple entities. Meaning:
mysite.com/{PlayerName} 转到 player.aspxmysite.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?
谢谢大卫
推荐答案
写两个constraints 返回布尔值是否段是一个团队/一个球员与否.
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 中开发的约束条件下运行.两种类型的应用程序(Forms 和 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 路由多个目的地的单一路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!