当我们在 ASP.NET 4 Beta 1 中加入网络形式路径时, 我们没有机会加入一些方法,
注:以下代码与我们添加的ASP.NET 4Beta 2的代码 " 类似 " 。 *那,** 代码的质量要高得多,但这只是一个样本!
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.Routing;
6:
7: public static class RouteExtensions
8: {
9: public static string GetUrlForRoute(this System.Web.UI.Page page, string routeName, RouteValueDictionary parameters)
10: {
11: VirtualPathData vpd= RouteTable.Routes.GetVirtualPath(null, routeName, parameters);
12: return vpd.VirtualPath;
13: }
14: public static string GetUrlForRoute(this System.Web.UI.Page page, RouteValueDictionary parameters)
15: {
16: VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, parameters);
17: return vpd.VirtualPath;
18: }
19:
20: public static void IgnoreRoute(this RouteCollection routes, string url)
21: {
22: routes.IgnoreRoute(url, null);
23: }
24:
25: public static void IgnoreRoute(this RouteCollection routes, string url, object constraints)
26: {
27: if (routes == null)
28: {
29: throw new ArgumentNullException("routes");
30: }
31: if (url == null)
32: {
33: throw new ArgumentNullException("url");
34: }
35: IgnoreRouteInternal internal3 = new IgnoreRouteInternal(url);
36: internal3.Constraints = new RouteValueDictionary(constraints);
37: IgnoreRouteInternal item = internal3;
38: routes.Add(item);
39: }
40:
41: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile)
42: {
43: return routes.MapRoute(name, url, physicalFile, null, null);
44: }
45:
46: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults)
47: {
48: return routes.MapRoute(name, url, physicalFile, defaults, null);
49: }
50:
51: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, string[] namespaces)
52: {
53: return routes.MapRoute(name, url, null, null, namespaces);
54: }
55:
56: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, object constraints)
57: {
58: return routes.MapRoute(name, url, physicalFile, defaults, constraints, null);
59: }
60:
61: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, string[] namespaces)
62: {
63: return routes.MapRoute(name, url, physicalFile, defaults, null, namespaces);
64: }
65:
66: public static Route MapRoute(this RouteCollection routes, string name, string url, string physicalFile, object defaults, object constraints, string[] namespaces)
67: {
68: if (routes == null)
69: {
70: throw new ArgumentNullException("routes");
71: }
72: if (url == null)
73: {
74: throw new ArgumentNullException("url");
75: }
76: if (physicalFile == null)
77: {
78: throw new ArgumentNullException("physicalfile");
79: }
80: Route route2 = new Route(url, new PageRouteHandler(physicalFile));
81: route2.Defaults = new RouteValueDictionary(defaults);
82: route2.Constraints = new RouteValueDictionary(constraints);
83: Route item = route2;
84: if ((namespaces != null) && (namespaces.Length > 0))
85: {
86: item.DataTokens = new RouteValueDictionary();
87: item.DataTokens["Namespaces"] = namespaces;
88: }
89: routes.Add(name, item);
90: return item;
91: }
92:
93: // Nested Types
94: private sealed class IgnoreRouteInternal : Route
95: {
96: // Methods
97: public IgnoreRouteInternal(string url)
98: : base(url, new StopRoutingHandler())
99: {
100: }
101:
102: public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary routeValues)
103: {
104: return null;
105: }
106: }
107: }
108:
那么,你如何使用这些方法?
这些扩展增加了一些简单的方法:
这些方法为界定网络形式路径提供了捷径。
路由集合. MapRoute( 字符串名称、 字符串 URL、 字符串物理文件) ;
例如,绘制您现在可以做的简单路径图 :
路线表. Routes. MappROute (“Production Details Route”、“产品/”、“~/productionDetails.aspx”);
还有几种变量, 允许定义违约等
路由集合. MapRoute( 字符串名称、 字符串 URL、 字符串物理文件、 对象默认) ;
路由集合. MapRoute( 字符名、 字符串 URL、 字符串物理页、 对象默认值、 字符串)[命名空间);
GetUrlForroute( 字符串路径Name, 路线估价参数) ;
使用一个有特定名称的路线和路由估价词汇, 包含多个参数, 将会给您返回一个包含要使用的 Url 的字符串。 在数据化应用程序中非常有用 !
例如,
<asp:hyperlink ID="HyperLink1" runat="server" NavigateUrl='<%# Page.GetUrlForRoute("ProductDetailsRoute",new RouteValueDictionary( new {productid= Eval("ProductID")}))%>' Text="Details"></asp:hyperlink>
GetUrlForROUT( 滚动价位参数) ;
同样的事情,但自动匹配 路线估价参数相对于路线
允许您定义URL( 用于升级 WebForms Apps ) 。 URL( 可用于升级 WebForms Apps )
路线集合. IgnoreROute( 字符串 URL) ;
路线集合. IgnoreRoute( 字符串 URL, 对象限制);
© 2026 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.