gpt4 book ai didi

asp.net - 当未提供路由名称时,使用 RouteLink 生成 url 的方法是什么?

转载 作者:行者123 更新时间:2023-12-02 21:26:44 25 4
gpt4 key购买 nike

使用@Html.RouteLink和路由名称只是指示路由引擎使用该名称的路由。这很容易理解,而且显然也是任何撰写博客文章或文档的人尝试解释 RouteLink 的唯一方法。

我很欣赏这是首选方法,而且我也一直在 RouteLink 中使用路线名称,但我想知道当我们不这样做时它是如何工作的,而且我找不到任何关于它的真正好的信息。所以问题的方法是:

public static MvcHtmlString RouteLink(
this HtmlHelper htmlHelper,
string linkText,
RouteValueDictionary routeValues
)

并与 MSDN 上所有有趣的内容保持一致,it's not explained at all .

Haacked.com has examples of using RouteLink以这种方式:

routes.MapRoute(
name: "Test",
url: "code/p/{action}/{id}",
defaults: new { controller = "Section", action = "Index", id = "" }
);

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);

@Html.RouteLink("Test", new {controller="section", action="Index", id=123})

@Html.RouteLink("Default", new {controller="Home", action="Index", id=123})

当我读到这篇文章时,我认为这是一个拼写错误,两者实际上都会绑定(bind)到“默认”,但实际上并没有作用。带有文本“Test”的链接生成为 https://localhost:44301/code/p/Index/123

这似乎“错误”,因为对我来说,默认值通常在未提供任何值时使用,但在这里它们在生成过程中用作查找值。这很奇怪而且很松散,我不记得曾经这样做过,也想不出为什么我会这样做。

如果我这样做:

 @Html.RouteLink("Test", new {controller="sectionA", action="Index", id=123})

它不起作用,但如果我这样做:

 @Html.RouteLink("Test", new {controller="section", action="IndexA", id=123})

它仍然如此,似乎只有 Controller 值很重要,但就生成的 url 而言,它完全未使用。

https://localhost:44301/code/p/Index/123

在 Haacked.com,菲尔说:

There’s even more details I’ve glossed over having to do with how a route’s default values figure into URL generation. That’s a topic for another time, but it explains why you don’t run into this problem with routes to controller actions which have an URL without parameters.

但他似乎没有回到那个话题。我已经查看了源代码,但我已经进行了 30 个调用,并且陷入了无处可去的抽象类,而且我现在无法从符号服务器中单步执行它。

那么这是否像“在 url 生成期间没有实际路由名称的情况下, Controller 名称成为路由名称”一样简单?如果是这样,他们为什么这样做/允许这样做?

最佳答案

看起来,如果未传递路由名称,@Html.ActionLink()@Html.RouteLink 会执行相同的操作。

看一下以下方法(位于 RouteCollectionExtensions.cs 中)。 name参数是这里的路由名称。

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))
{
usingAreas = false;
return routes.GetVirtualPath(requestContext, name, values);
}
string areaName = (string) null;
if (values != null)
{
object obj;
if (values.TryGetValue("area", out obj))
areaName = obj as string;
else if (requestContext != null)
areaName = AreaHelpers.GetAreaName(requestContext.RouteData);
}
RouteValueDictionary values1 = values;
RouteCollection routeCollection = RouteCollectionExtensions.FilterRouteCollectionByArea(routes, areaName, out usingAreas);
if (usingAreas)
{
values1 = new RouteValueDictionary((IDictionary<string, object>) values);
values1.Remove("area");
}
return routeCollection.GetVirtualPath(requestContext, values1);
}

您可以看到,根据路由名称是否为空,它会采取不同的操作。

为了回答您的问题,@Html.ActionLink()@Html.RouteLink() 的行为将是相同的(它们将选择相同的路线)基于配置)如果未传递路由名称。

更新

我不确定 Controller 名称如何成为路由名称,因为在以下方法调用中您可以看到操作名称和 Controller 名称都传递了空值:

public static string GenerateRouteLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
return HtmlHelper.GenerateLinkInternal(requestContext, routeCollection, linkText, routeName, (string) null, (string) null, protocol, hostName, fragment, routeValues, htmlAttributes, false);
}

关于asp.net - 当未提供路由名称时,使用 RouteLink 生成 url 的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28429738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com