问题描述
我想知道我们应该在AJAX上使用什么URL来在外部C#类中调用WebMethod
I would like to know what url should we use on AJAX to call a WebMethod in an external C# class
要在AJAX后面的页面代码上调用 [WebMethod]
,我们使用:
To call a [WebMethod]
on a page's code behind by AJAX we use:
url: 'default.aspx/Method'
但是我无法访问 MyClass.cs (位于/foo/中)中的 [WebMethod]
But i am being unable to access a [WebMethod]
in MyClass.cs (located in /foo/)
例如,这些都不起作用:
Those, for example, don't work:
url: 'default.aspx/MyClass.Method'
url: 'foo/MyClass.cs/Method'
如何在外部C#类文件上访问 WebMethod ?
How can i access a WebMethod on an external C# class file?
推荐答案
您需要添加一个可访问Web的文件与外部类进行交互,以便从AJAX调用中访问它们.您可以使用诸如asmx(ASP.Net Web服务)之类的方法来公开Web方法.该文件基本上只是指向类文件的标记占位符.内容就是:
You need to add a web accessible file to interact with the external classes in order to access them from AJAX calls. You could use something like an asmx (ASP.Net web service) that exposes the webmethods. The file is basically just a markup place holder that points at a class file. Contents are just:
<%@ WebService Language="C#" CodeBehind="~/foo/MyClass.cs" Class="MyClass" %>
然后您的类必须继承自 System.Web.Services.WebService
,您应该会很好.
Then your class has to inherit from System.Web.Services.WebService
and you should be good.
如果您从Visual Studio中添加文件并添加Web服务文件,则可以获取它来为您创建所有这些文件.
If you do an add file from Visual Studio and add a web service file you can get it to create all this for you.
这篇关于在AJAX调用中在外部C#类中访问WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!