使用 url 片段创建 T4MVC ActionLink

发布于 2024-09-03 08:16:14 字数 456 浏览 12 评论 0原文

有没有办法创建一个强类型的 T4MVC ActionLink,其中包含哈希/磅/片段?

例如,这是我想要创建的链接:

<a href="/Home/Index#food">Feed me</a>

但是 T4MVC 对象没有扩展可以执行此操作。

<%= Html.ActionLink("Feed me", T4MVC.Home.Index()) %>

所以,我最终要做的就是创建一个动作,然后以这种方式嵌入它:

<a href="<%= Url.Action(T4MVC.Home.Index()) %>"#food>Feed me</a>

这不是很理想。有人有什么想法/建议吗?

提前致谢

Is there a way to create a strongly typed T4MVC ActionLink with a hash/pound/fragment in it?

For example, here is the link I'd like to create:

<a href="/Home/Index#food">Feed me</a>

But there's no extension to the T4MVC object that can do this.

<%= Html.ActionLink("Feed me", T4MVC.Home.Index()) %>

So, what I end up having to do is create an action, and then embed it that way:

<a href="<%= Url.Action(T4MVC.Home.Index()) %>"#food>Feed me</a>

This isn't very desirable. Anyone have any ideas/suggestions?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺孤伤于心 2024-09-10 08:16:14

在 ASP.NET MVC 2.0 中添加了新的帮助程序,允许您指定片段< /a>.例子:

<%= Html.ActionLink("Feed me", "Action", "Controller", null, null, "food", null, null) %>

In ASP.NET MVC 2.0 new helpers have been added that allow you to specify the fragment. Example:

<%= Html.ActionLink("Feed me", "Action", "Controller", null, null, "food", null, null) %>
放低过去 2024-09-10 08:16:14

这种方法是我能想到的唯一一种感觉(对我来说)比手动编写锚点稍微好一点的方法:

${Html.ActionLink("Feed me", T4MVC.Home.Index(), Fragment: "food")}

除了 Spark ViewEngine - 它花费了 1 个很好的旧 htmlhelper 扩展方法和 1 个旧的 htmlhelper 扩展方法。命名参数。


我认为这在默认视图引擎中不可用?我决定为 ActionLink 编写一个快速扩展,但它并不优雅,而且我希望在 T4MVC 的未来版本中可以为其他人提供任何解决方案。

Spark 替换 <%=%>与 ${}。提到它只是因为我更喜欢它(如果你强调代码优雅,你应该尝试它)。需要 C# 4.0 才能使用命名参数。

那是因为我想避免丢失参数“food”参数映射的信息。


是的,我非常同意马蒂亚斯·雅各布森的观点。

This kind of approach is the only one i can think of that feels (to me) slightly better than writing anchor manually:

${Html.ActionLink("Feed me", T4MVC.Home.Index(), Fragment: "food")}

Apart from spark viewengine - it costs 1 good old htmlhelper extension method & named parameters.


I assume that this isn't available in the default viewengine? I've decided to write a quick extension for the ActionLink, but it's not elegant, and I would have liked any solution to be available to others in future versions of T4MVC.

Spark replaces <%=%> with ${}. Mentioned just because I prefer it (You should try it if You emphasize code elegance). C# 4.0 is required in order to use named parameters.

That's because I would like to avoid losing information to which parameter "food" argument maps.


And yeah, i strongly agree with Mattias Jakobsson.

落花随流水 2024-09-10 08:16:14

更新:此重载包含在 T4MVC 2.6.56 中。

是的,为了完整性,我们应该将其添加到 T4MVC 中。这应该很容易做到,但如果我们也开始添加协议/主机名,我们最终会出现大量过载。

如果我们停止支持 Fx 3.5,事情会更容易,因为我们可以依赖默认/命名参数,这对减少过载地狱有很大帮助。但到目前为止我一直在避免这一步,因为并不是每个人都使用 4.0。

也许我应该将当前版本冻结为最后一个 Fx 3.5/MVC 1.x 兼容版本,然后在较新的版本中仅支持 Fx 4.0/MVC 2(同时无限期地保留旧版本)。无论如何,我离题了:)

Update: This overload is included with T4MVC 2.6.56

Yes, for completeness we should probably add this to T4MVC. It should be easy to do, except we'll end up with a lot of overload if we start adding protocol/hostname as well.

Things would be easier if we stopped supporting Fx 3.5, because we could rely on default/named params, which help a lot with reducing overload hell. But I've been avoiding that step so far because not everyone is on 4.0 yet.

Maybe I should freeze the current version as the last Fx 3.5/MVC 1.x compatible, and then only support Fx 4.0/MVC 2 in newer builds (while keeping the old one up indefinitely). Anyway, I'm digressing :)

爱的十字路口 2024-09-10 08:16:14

David Ebbo 根据我在 StackOverflow 上提出的类似问题将此功能添加到 T4MVC 中。

David Ebbo added this feature to T4MVC based on a similar question I made on StackOverflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文