问题
I would like to know what url should we use on AJAX to call a WebMethod in an external C# class
To call a [WebMethod]
on a page's code behind by AJAX we use:
url: 'default.aspx/Method'
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'
How can i access a WebMethod on an external C# class file?
回答1:
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" %>
Then your class has to inherit from System.Web.Services.WebService
and you should be good.
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.
来源:https://stackoverflow.com/questions/38363623/access-webmethod-in-external-c-sharp-class-on-ajax-call