访问.cs文件中声明的web方法与ajax中的任何aspx或ascx文件无关(jquery)

您好我将一个Web方法从一个aspx页面的代码隐藏文件移动到另一个cs文件,该文件存在于数据部分(不包含任何aspx页面)。 以前我曾经通过使用Ajax访问web方法,类似于url

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

但现在我正在尝试使用Url访问移动的Web方法

 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) { 

但我收到错误,我不知道如何访问.cs文件中声明的Web方法,该文件不包含任何与之关联的aspx文件,请帮助我。

我的网络方法就像

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

它们被称为ASP.NET AJAX页面方法,原因是,端点必须是public static方法,使用WebMethod属性进行修饰,这些方法位于从Page派生的Page类或类中。

试试这个

  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);