如何从 ASP.NET MVC 框架中的 AjaxOptions 获取生成的 JavaScript?

发布于 2024-08-06 04:04:59 字数 550 浏览 4 评论 0原文

我正在尝试 ASP.NET MVC 框架,并想创建一个 ajax 帮助器方法。基本上,这个助手的行为类似于 ActionLink,但不对其链接文本进行编码。编写 HtmlHelper 相当简单,您只需编写自己的GenerateLinkInternal 版本即可。但这对于 AjaxHelpers 不起作用,因为GenerateLink 的ajax 版本间接调用内部的ToJavascriptString(通过GenerateAjaxScript),因此不能在MVC 程序集外部调用。我当然可以重写整个事情,但这似乎有点矫枉过正,有更好的方法吗?

最终,我想让这个助手像 BeginForm 一样运行,使链接包围 HTML 块。我还没有看过它,但我假设它也使用 ToJavascriptString 。我在网上搜索并浏览了 MVC 源代码,我开始怀疑我是否完全走错了路。

谢谢

更新:我越看这个问题,就越觉得根本没有解决方案。编写 MVC 框架的人并没有考虑过帮助人们编写自己的助手!

更新:我最终编写了一个几乎重复 AjaxOptions 功能的助手。

I'm trying out ASP.NET MVC Framework and would like to create an ajax helper method. Basically this helper would act like ActionLink but without encoding its link text. It is rather trivial to write as an HtmlHelper, you simply have to write your own version of GenerateLinkInternal. This isn't working for AjaxHelpers though, as the ajax version of GenerateLink is indirectly calling ToJavascriptString (through GenerateAjaxScript) which is internal, thus cannot be called outside the MVC assembly. I sure can rewrite the whole thing, but it seems way overkill, is there a better way?

Ultimately, I'd like to make this helper act like BeginForm to make the link surround a block of HTML. I've not looked at it yet, but I assume that it uses ToJavascriptString too. I've searched the web and, looking through the MVC source code, I begin to wonder if I'm completely on the wrong track.

Thanks

Update: The more I look at this problem, the more I think that there's simply no solution. Whoever wrote the MVC Framework didn't think about helping people write their own helpers!

Update: I've ended up writing an helper that pretty much duplicate AjaxOptions functionality.

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

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

发布评论

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

评论(1

听闻余生 2024-08-13 04:04:59

您可以通过从头开始编写自己的帮助程序来更轻松地做到这一点(即不要调用任何 Html.ActionLink()/Ajax.ActionLink() 方法),只需使用 Url.Action() 即可。

例如,这样做非常简单:

public static string NonEncodedUrl(this HtmlHelper helper, 
    string linkAction, string text)
{
    // Get a new UrlHelper instance in the current context
    var url = new UrlHelper(helper.ViewContext.RequestContext);

    return String.Format("<a href=""{0}"">{1}</a>", url.Action(linkAction), text);
}

您当然可以使用重载和额外参数来扩展它,以满足您自己的需求。

You could probably do this a lot easier by writing your own helper from scratch (i.e. don't make calls to any of the Html.ActionLink()/Ajax.ActionLink() methods) simply by using Url.Action() instead.

For example, it's pretty trivial to do this:

public static string NonEncodedUrl(this HtmlHelper helper, 
    string linkAction, string text)
{
    // Get a new UrlHelper instance in the current context
    var url = new UrlHelper(helper.ViewContext.RequestContext);

    return String.Format("<a href=""{0}"">{1}</a>", url.Action(linkAction), text);
}

You can of course extend this with overloads and extra parameters to suit your own needs.

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