2013-05-21 53 views
0

我有一個AJAX調用,它完美地適用於本地服務器。我試圖通過C#(從另一個應用程序/域)進行相同的調用,但我很困惑。將AJAX REST調用轉換爲C#.NET

這是我的webmethod:

[WebMethod(true)] 
public static object ParentLogin(string email, string password) 
{ 
    List<object> ChildrenList = new List<object>(); 

    Usuario loggedUser = GetByEmailAndPass(email, password); 
    if (loggedUser != null) 
    { 
     var children = Factory.Current.GetInstance<IChildrenBIZ>().GetAll().ToList(); 

     foreach (var item in children) 
     { 
      ChildrenList.Add(new { userid = item.Id, name = item.Nome }); 
     } 
     return new { success = true, email = loggedUser.Login, users = ChildrenList}; 
    } 

    return new { success = false }; 
} 

這是我的Ajax調用:

var url = "http://localhost/" + "LoginTest/Login.aspx/ParentLogin"; 
    var dataParams = "{ email: " + "'[email protected]'" + ", password: " + "'123123'" + " }"; 

    ExecuteAjaxCall(url, dataParams, true, function (msg) { 
     if (msg.d) { 
      var success = msg.d.success; 
      var children = msg.d.users; 

      if (children.length > 0) { 
       $.each(children, function (i, item) { 
        if (item) { 

         var childID = item.userid; 
        } 
       }); 
      } 
     } 

    }, StandardError); 

}); 

現在我想後面通過調用這個代碼相同Webthod。我嘗試了Restsharper,但我無法實現。 任何人,請嗎?

編輯:

這是ExecuteAjaxCall方法:

function ExecuteAjaxCall(url, dataParam, isAssync, callBackSucess, callBackError) { 
$.ajax({ 
    type: "POST", 
    url: url, 
    data: dataParam, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    assync: isAssync, 
    success: function (msg) { 
     callBackSucess(msg); 
    }, 
    error: function (msg) { 
     callBackError(ERROR_MSG); 
    } 
}); 
} 

重要:

主要的原因我想這是bacause我想消費從另一個域ParentLogin方法。也許從iOS或Android應用程序!這是正確的方法嗎?

謝謝!

+0

這是一個網站,還是一個Web應用程序項目? –

+0

如果您使用的是現成的框架(如JQuery),您可以發佈ExecuteAjaxCall的代碼,或者爲我們確定它所在的JS庫嗎? –

+0

John Saunders:這是一個Web應用程序。 – user2407404

回答

3

此Web方法(實際上是一種「頁面方法」)是您的Web應用程序中的一種公共靜態方法。你應該能夠叫它:

namespace.Login.ParentLogin(email, password); 
+0

謝謝。我添加了更多關於我的問題的信息。我需要能夠從另一個域/應用程序/平臺調用此方法。 – user2407404

1

我會重構出一個服務類來保存你的應用程序邏輯,所以你可以在另一個項目,而不是RestSharp使用它。

[WebMethod(true)] 
public static object ParentLogin(string email, string password) 
{ 
    return ServiceAdapter.ParentLogin(email, password); 
} 

public interface IService 
{ 
    object ParentLogin(string email, string password); 
} 

public sealed class Service : IService 
{ 
    public object ParentLogin(string email, string password) 
    { 
     List<object> ChildrenList = new List<object>(); 

     Usuario loggedUser = GetByEmailAndPass(email, password); 
     if (loggedUser != null) 
     { 
      var children = Factory.Current.GetInstance<IChildrenBIZ>().GetAll().ToList(); 

      foreach (var item in children) 
      { 
       ChildrenList.Add(new { userid = item.Id, name = item.Nome }); 
      } 
      return new { success = true, email = loggedUser.Login, users = ChildrenList}; 
     } 

     return new { success = false }; 
    } 

    //TODO move GetByEmailAndPass 
} 

public sealed class ServiceAdapter 
{ 
    public static readonly IService Service = new Service(); 
} 
+0

如果你的項目很簡單,那麼這是過分慫恿蛋糕,但總的來說,我完全同意,這是我接觸這個問題的首選方法。 +1爲偉大的思想思想相似:) –

+0

謝謝!我添加了更多關於我的問題的信息。我需要能夠從另一個域/應用程序/平臺調用此方法。 – user2407404

+0

如果你想使用C#跨平臺,你應該使用['Xamarin'](http://xamarin.com/)。如果你想使用本地語言,那麼你將需要像你在做什麼一樣製作一個API。 – Romoku