2

嗨,我将 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<RefEmployee> EmployeeSummaryHistory(string empNo)
{
    var employee = new RefEmployeeRepository();
    //Employee History.
    List<RefEmployee> list = new List<RefEmployee>();
    list = employee.SummaryHistEmployee(empNo);
    return list;
}
4

2 回答 2

2

出于某种原因,它们被称为 ASP.NET AJAX 页面方法,端点必须是public static用属性修饰的方法,WebMethod它们位于PagePage.

于 2013-11-04T21:03:30.523 回答
0

试试这个

    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);
于 2013-11-04T21:55:56.700 回答