实现此目的的一种方法是通过继承IRouteConstraint创建自定义RouteConstraint并将您的url保存在xml中。你需要知道的页面模板类型,所以你可以在这样一个枚举存储这样的信息:
public enum TemplateType
{
Home,
Product,
Category
}
下面是一个示例XML,你可以用它来存储数据:
<Sitemap>
<Item url="/home" TemplateType="Home" />
<Item url="/products/category" TemplateType="Category">
<Item url="/products/category/product" TemplateType="Product" />
</Item>
</Sitemap>
后您将需要方法来提取Sitemap节点并获取特定节点。您只需要反序列化xml并遍历它即可找到特定的url。
您的自定义RouteConstraing应该是这样的:
public class CustomRouteConstraint : IRouteConstraint
{
#region IRouteConstraint Members
private TemplateType m_type;
public CustomRouteConstraint(TemplateType type)
:base()
{
m_type = type;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
bool returnValue = false;
SitemapNode sitemapNode = GetSiteMapNode(httpContext.Request);
if (sitemapNode != null && sitemapNode.TemplateType == m_type)
{
return true;
}
return returnValue;
}
#endregion
private static SitemapNode GetSiteMapNode(HttpRequestBase request)
{
//get the aboslute url
string url = request.Url.AbsolutePath;
return SitemapManager.GetSiteMapNode(url);
}
}
后你在的RegisterRoutes方法的Global.asax文件都的这地方,你需要做这样的事情:
routes.MapRoute(
"", // Route name
route, // URL with parameters
new { lang = "en", region = "us", controller = "Category", action = "Index" },
new { param1 = new CustomRouteConstraint(TemplateType.Category) });
希望这会有所帮助。