我知道在MVC框架中,你有Html类来创建URL:
Html.ActionLink("About us", "about", "home");
但是如果你想在Webforms中生成Urls呢?
我没有找到关于使用Webforms生成URL的详细信息.
例如,如果我正在生成这样的路线:
Route r = new Route("{country}/{lang}/articles/{id}/{title}", new ArticleRouteHandler("~/Forms/Article.aspx")); Route r2 = new Route("{country}/{lang}/articles/", new ArticleRouteHandler("~/Forms/ArticlesList.aspx")); Routes.Add(r); Routes.Add(r2);
如何使用路由表数据生成URL.
例如./ ca / en/articles/123 /文章标题没有
谢谢你的回答.再补充一点,这就是我所做的:
RouteValueDictionary rvdSiteDefaults = new RouteValueDictionary { { "country", "ca" }, { "lang", "en" } }; Route oneArticle = new Route("{country}/{lang}/articles/a{id}/{title}", rvdSiteDefaults, rvdConstrainID, new ArticleRouteHandler("~/Articles/Details.aspx")); Routes.Add( "Article", oneArticle);
public static string CreateUrl(Article a) { // Note, Article comes from Database, has properties of ArticleID, Title, etc. RouteValueDictionary parameters; string routeName = "Article"; // Set in Global.asax parameters = new RouteValueDictionary { { "id", a.ArticleID }, { "title", a.Title.CleanUrl() } };
CleanUrl()返回URL友好名称.
VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, routeName, parameters); string url = vpd.VirtualPath; return url; // eg. /ca/en/1/The-Article-Title }
田田!