c# - 将 URL 片段添加到 MVC ActionLink

标签 c# asp.net-mvc

这是我的部分代码

这个

<%= Html.ActionLink(Model[x].Title, "Index", "q", new { slug = Model[x].TitleSlug, id = Model[x].PostID }, null)%>

生成这个 url

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4

但我想生成一个带有片段的 url,如下所示:

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4#1

有没有办法使用 HTML.ActionLink 函数来做到这一点?

最佳答案

ActionLink 有两个采用片段参数的“大型重载”:

public static string ActionLink(this HtmlHelper htmlHelper,
     string linkText, string actionName, string controllerName,
     string protocol, string hostName, string fragment, object routeValues,
     object htmlAttributes);
public static string ActionLink(this HtmlHelper htmlHelper,
     string linkText, string actionName, string controllerName,
     string protocol, string hostName, string fragment,
     RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes);

参见 MSDN有关重载的更多信息。

在您的情况下(并特别注意“片段”参数):

<%= Html.ActionLink(Model[x].Title, "Index", "q",
     /* protocol */ null, /* hostName */ null, /* fragment */ "1",
     new { slug = Model[x].TitleSlug, id = Model[x].PostID }, null) %>

通过“大型重载”,您可以将大多数参数值保留为 null,它们将获得适当的默认值。

关于c# - 将 URL 片段添加到 MVC ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1996232/

相关文章:

c# - 声明有界类型的泛型字典

c# - 如何使用 C# SMTP 客户端对 Domainkeys/DKIM 电子邮件进行签名?

asp.net-mvc - "GenerateResource"任务意外失败空引用异常

javascript - asp.net mvc 3 在 javascript 中访问 DateTime 模型属性

.net - 从 Controller 返回一个 EditorTemplate 作为局部 View

c# - 使用自定义绘制的控件滚动面板

c# - 如何从 C# 中的响应中提取和获取值

c# - MSSQL DB - 在数据库中建立关系有什么好处?

asp.net - 将 MS Ajax Minfier 4.0 用于 Asp.Net MVC 2.0?

asp.net-mvc - MVC.NET 站点 SSL - 站点无法生成安全连接