这是我的部分代码
这个
<%= 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/