Access WebMethod in external C# class on AJAX call

北城以北 提交于 2019-12-12 07:05:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!