将URL片段添加到MVC ActionLink

Luk*_*101 16 c# asp.net-mvc

这是我的代码的一部分

这个

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

产生这个网址

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:61158/q/is_there_another_indiana_jones_movie_in_the_works/4#1
Run Code Online (Sandbox Code Playgroud)

有没有办法使用HTML.ActionLink函数执行此操作?

Eil*_*lon 24

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);
Run Code Online (Sandbox Code Playgroud)

有关重载的更多信息,请参阅MSDN.

在你的情况下它将是(并特别注意"片段"参数):

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

使用"mega overloads",您可以将大多数参数值保留为null,并且它们将获得适当的默认值.