使用 url 片段创建 T4MVC ActionLink
有没有办法创建一个强类型的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 ASP.NET MVC 2.0 中添加了新的帮助程序,允许您指定片段< /a>.例子:
In ASP.NET MVC 2.0 new helpers have been added that allow you to specify the fragment. Example:
这种方法是我能想到的唯一一种感觉(对我来说)比手动编写锚点稍微好一点的方法:
除了 Spark ViewEngine - 它花费了 1 个很好的旧 htmlhelper 扩展方法和 1 个旧的 htmlhelper 扩展方法。命名参数。
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:
Apart from spark viewengine - it costs 1 good old htmlhelper extension method & named parameters.
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.
更新:此重载包含在 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 :)
David Ebbo 根据我在 StackOverflow 上提出的类似问题将此功能添加到 T4MVC 中。
David Ebbo added this feature to T4MVC based on a similar question I made on StackOverflow.