當你看看源代碼,操作鏈接你發現
<%= Html.ActionLink("LinkText", "Action", "Controller"); %>
將匹配
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName) {
return ActionLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary());
}
現在到目前爲止,這看起來不錯,因爲它正在創造一個新的路徑值字典,以便它不會將當前上下文中的值傳遞給新鏈接,這將會發生。
然而,在代碼進一步下降,其中正在生成的網址:
public static string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues) {
if (routeCollection == null) {
throw new ArgumentNullException("routeCollection");
}
if (requestContext == null) {
throw new ArgumentNullException("requestContext");
}
RouteValueDictionary mergedRouteValues = RouteValuesHelpers.MergeRouteValues(actionName, controllerName, requestContext.RouteData.Values, routeValues, includeImplicitMvcValues);
VirtualPathData vpd = routeCollection.GetVirtualPathForArea(requestContext, routeName, mergedRouteValues);
if (vpd == null) {
return null;
}
string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
return modifiedUrl;
}
可以看到的RequestContext被引用其中有訪問的RouteData和routeCollections,其中將包含ID數據。當創建VirtualPathForArea,下面一行是其中的id值將出現在您的網址:
internal static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, string name, RouteValueDictionary values, out bool usingAreas) {
if (routes == null) {
throw new ArgumentNullException("routes");
}
if (!String.IsNullOrEmpty(name)) {
// the route name is a stronger qualifier than the area name, so just pipe it through
usingAreas = false;
return routes.GetVirtualPath(requestContext, name, values);
}
string targetArea = null;
if (values != null) {
object targetAreaRawValue;
if (values.TryGetValue("area", out targetAreaRawValue)) {
targetArea = targetAreaRawValue as string;
}
else {
// set target area to current area
if (requestContext != null) {
targetArea = AreaHelpers.GetAreaName(requestContext.RouteData);
}
}
}
// need to apply a correction to the RVD if areas are in use
RouteValueDictionary correctedValues = values;
RouteCollection filteredRoutes = FilterRouteCollectionByArea(routes, targetArea, out usingAreas);
if (usingAreas) {
correctedValues = new RouteValueDictionary(values);
correctedValues.Remove("area");
}
VirtualPathData vpd = filteredRoutes.GetVirtualPath(requestContext, correctedValues);
return vpd;
}
行:
VirtualPathData vpd = filteredRoutes.GetVirtualPath(requestContext, correctedValues);
需要的虛擬路徑(這只是路線)並返回它。因此,虛擬路徑將是/Home/About/Flib
然後當返回的虛擬路徑下面一行用它來設置客戶端的URL跳轉鏈接:
string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
所以幾乎是它看起來像ActionLink的設置使用區域的虛擬路徑,這只是匹配的路線,在這種情況下恰好是默認路線。
@Carl,@amurra:這是不是解決問題或者這只是中無法充分解決的症狀的解釋嗎? – 2011-02-24 20:03:28
@Robert Koritnik - 這只是對ASP.NET MVC 2中發生了什麼的解釋。不知道這段代碼是否隨ASP.NET MVC 3發生了變化。 – amurra 2011-02-24 20:27:40
我不知道,老實說我並不在乎,因爲我也使用MVC2 RTM(因爲我必須使用.net 3.5)。所以我也有這個問題。但我做了這似乎工作:http://stackoverflow.com/questions/3475981/ambient-values-in-mvc2-net-routing/5110032#5110032 – 2011-02-24 20:30:33