我如何调用用户控件中定义的[WebMethod]?

发布于 2024-09-08 22:58:25 字数 151 浏览 0 评论 0原文

我正在使用 C# 在 Asp.Net 中工作,并且在用户控件中定义了一个 [WebMethod] 。我不想使用网络服务。

如何调用带有 [WebMethod] 属性的方法?我想从 jQuery (Ajax) 请求调用它。

I am working in Asp.Net using C# and I have a [WebMethod] defined in a User Control. I don't want to use a web service.

How can I call a method with an [WebMethod] attribute? I want to call it from a jQuery (Ajax) request.

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

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

发布评论

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

评论(3

任性一次 2024-09-15 22:58:25

如果您的目标是集中共享功能,您确实应该重新考虑使用服务。

ASMX 服务没有 WCF 服务所具有的任何配置负担(这通常是现在有人提到“Web 服务”时想到的)。如果您使用简单的 ASMX“ScriptService”,代码、开发、配置等将与您现在在 ASPX 文件中所做的几乎相同,只是集中化。 ASMX ScriptServices 和 ASPX 页面方法非常相似,以至于它们实际上共享大部分相同的底层后端代码。

You really should reconsider using a service if your goal is to centralize shared functionality.

ASMX services don't have any of the configuration burden that WCF services do (which is often what comes to mind when someone mentions "web service" now). If you use a simple ASMX "ScriptService", the code, development, configuration, etc will be nearly identical to what you're doing in ASPX files now, just centralized. ASMX ScriptServices and ASPX page methods are so similar that they literally share most of the same underlying backend code.

忘羡 2024-09-15 22:58:25

您不能调用 ASCX 用户控件中声明的页面方法。页面方法必须在您的 aspx 中声明或将其公开为正确的 asmx Web 服务。

You cannot call a page method declared within an ASCX user control. The page method would have to be declared in your aspx or expose it as a proper asmx web service.

筱果果 2024-09-15 22:58:25

理论上你可以,但它会变得有点混乱,我不会推荐它。
但是您可以做的是调用包含该控件的 aspx 页面并传递一个查询字符串参数(如 runfunc=1),该参数将由 ascx 控件处理并运行该函数。
否则,所有其他功能都会运行 - 并且您可以将所有 html 放在一个面板中,如果您调用“WebMethod”函数,该面板将被隐藏。
这还允许您删除函数名称上的 WebMethod 属性。

You theoretically could, but it would get slightly messy and i wouldnt recommend it.
But what you could do is call the aspx page containing the control and pass a querystring parameter (like runfunc=1) which would be handled by the ascx control and run the function.
All other functionality would be run otherwise - and you could put all the html in a panel which is hidden if you called the "WebMethod" function.
This would also allow you to remove the WebMethod property on the function name.

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