添加一个网址片段到ActionLink的MVC [英] Adding a URL Fragment to an MVC ActionLink

查看:113
本文介绍了添加一个网址片段到ActionLink的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code的一部分

here is part of my code

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

产生这个网址

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

但我想产生一个网址片段,像这样的:

but I want to produce a url with a fragment, like this:

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

有没有办法做到这一点使用的HTML.ActionLink功能?

Is there a way to do this using the HTML.ActionLink function?

推荐答案

有ActionLink的那取片段参数两个巨型重载

There are two "mega overloads" of ActionLink that take a fragment parameter:

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 有关重载更多信息。

See MSDN for more info on the overloads.

在你的情况下,这将是(和特别注意到了片段参数):

In your case it would be (and note the "fragment" parameter in particular):

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

随着大型重载你可以离开大多数参数值无效,他们将获得相应的默认值。

With the "mega overloads" you can leave most parameter values as null and they will get the appropriate default values.

这篇关于添加一个网址片段到ActionLink的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆