Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some help here. I know how to call a Page method/Webservice Method using jquery ajax but i would like to know how to call .NET class method to avoid copying codes from handler/page method or service method.

I have a class declared with static methods all decorated with webmethod attributes. I have tried it but i get an error 500. I know it's possible to call an action method in MVC but i'm working on a webforms project and would like to how to do it in webforms.

Any help would be highly appreciated.

Thanks

What I have tried:

.Net Class
C#
<pre>
using System.Web.Services;
using System.Web.Script.Services;

public class Container
{
    public int Name { get; set; }
    public int Size {get;set;}

    [WebMethod()]
    [ScriptMethod()]
    public static bool Exists(string ContainerName)
    {
        // Check stuff here and return true or false;
        return true;
    }
}



Jquery Code

JavaScript
$(document).ready(function () {
                            $('#testModel').click(function (e) {
                                // alert("calling web service now");

                                $.ajax({
                                    url: 'Models/Container/Exists',
                                    method: 'post',
                                    contentType: 'application/json; charset=utf-8;',
                                    data: '{ "ContainerName": "xxxx" }',
                                    success: function (r) {
                                        alert(r.d);
                                    },
                                    error: function (err) {
                                        alert(err.status + "\nScriptCall Failed");
                                    }
                                });
                            });
                        });
Posted
Comments
Bryian Tan 12-Oct-17 14:58pm    
Is this a web form? The URL should be Container.aspx/Exists . Assuming the page name is Container.aspx
Donnie N.I 13-Oct-17 3:59am    
Yes it's web forms. I have a class declared inside "Models" Folder called Container and i'm try to call the static method Exists to save me from copying the code to a handler or page method.
Richard Deeming 12-Oct-17 15:10pm    
AFAIK, you can't do that. ASP.NET has no way of mapping the path Models/Container to your class, because it isn't an HTTP handler.
Donnie N.I 13-Oct-17 4:01am    
Ok. Thanks... So the best way to be to put in a HTTP handler or page method? For me that's code duplication and i'm trying so hard to avoid copying codes...
Richard Deeming 13-Oct-17 7:53am    
I'd be inclined to use a Web Service[^].

You don't need to duplicate the code. Your Web Service methods can simply call the existing methods.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900