2

こんにちは、web メソッドを aspx ページのコード ビハインド ファイルからデータ セクション (aspx ページを含まない) にある別の cs ファイルに移動しました。以前は、Ajax を使用して Web メソッドにアクセスしていました。

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

しかし、エラーが発生し、関連する aspx ファイルを含まない .cs ファイルで宣言された Web メソッドにアクセスする方法がわかりません。これを手伝ってください。

私のWebメソッドは次のようなものです

[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、属性で装飾されたメソッドである必要があります。WebMethodPagePage

于 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 に答える