与 System.Web.Mvc 版本具有相同方法签名的自定义 HtmlHelper - 不明确的调用
我在 MVC2 源代码中找到了 LabelFor() Html Helper 的稍微定制版本。当我在视图中使用它时,我收到“不明确的调用”错误 - 考虑到我还保留了原始签名,这是完全有道理的。
LabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression)
有没有办法在视图中设置 using/import 别名?
I have a slightly customized version of the LabelFor() Html Helper found in the MVC2 sources. When I use it in a view I get an "Ambiguous Invocation" error - which makes perfect sense given that my also retains the signature of the original.
LabelFor<TModel, TValue>(this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression)
Is there a way to set up a using/import alias within the view?
Edit: Unfortunately this suggests it's not possible. It looks like creating extension methods with identical method names and signatures works in general but apparently not in aspx templates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能这样做,因为除了通过目标类型和解析中涉及的参数之外,没有任何机制可以影响扩展方法解析。在这种情况下,您将不得不求助于搜索和搜索。代替。
另一种方法是定义您自己的页面基本类型,覆盖 Html 属性以返回您自己的派生 HtmlHelper 并以此为基础进行扩展。但这似乎更黑客。
You can't do this because theres no mechanism to influence extension method resolution other than via the types of the target and the parameters involved in the resolution. In this case you will have to resort to search & replace.
An alternative would be to define your own page base type that overwrites the Html property to return your own derived HtmlHelper and base the extension off that. But that seems even hackier.