accessing web method declared in .cs file not associated to any aspx or ascx file in ajax(jquery)

Hi I moved a web method from code behind file of an aspx page to another cs file which is present in data section(which doesn’t contain any aspx page). Previously I used to access web method by using Ajax, the url like

type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "Results.aspx/EmployeeSummaryHistory",   // call history function
data: JSON.stringify(emp),
success: function (resp) {

but now I am trying to access the moved web method by using the Url

type: "post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "~/Model/Data/EmployeeRepository.cs/EmployeeSummaryHistory",   // call history function
data: JSON.stringify(emp),
success: function (resp) {

but I am getting error and I don’t know how to access web method declared in .cs file which doesn’t contain any aspx file associated to it please help me with this.

My web method is like

[WebMethod]
public static List<RefEmployee> EmployeeSummaryHistory(string empNo)
{
    var employee = new RefEmployeeRepository();
    //Employee History.
    List<RefEmployee> list = new List<RefEmployee>();
    list = employee.SummaryHistEmployee(empNo);
    return list;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

They are called ASP.NET AJAX Page Methods for a reason, the endpoint must be public static methods, decorated with the WebMethod attribute, that are within a Page class or class that derives from Page.

Method 2

Try this

    var theWebRequest = HttpWebRequest.Create("http://localhost:51045/Default.aspx/Senddata");
                theWebRequest.Credentials = new NetworkCredential(tobj.Username, tobj.Password,tobj.propertyID);
                theWebRequest.Method = "POST";
                theWebRequest.ContentType = "application/json; charset=utf-8 ";
                //theWebRequest.Headers.Add(HttpRequestHeader.Pragma.ToString, "no-cache");
                using (var writer = theWebRequest.GetRequestStream())
                {
                    string json = new JavaScriptSerializer().Serialize(new
                    {
                        something = value                    
                    });

                    var data = Encoding.UTF8.GetBytes(json);

                    writer.Write(data, 0, data.Length);
                    writer.Flush();
                    writer.Close();
                }

                var theWebResponse = (HttpWebResponse)theWebRequest.GetResponse();         
                var theResponseStream = new StreamReader(theWebResponse.GetResponseStream());
                string result = theResponseStream.ReadToEnd().ToString();
   var data1 = new JavaScriptSerializer().DeserializeObject(result);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x