Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

Access WebMethod in external C# class on AJAX call

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...