【问题标题】:Converting AJAX REST call to a C# .NET将 AJAX REST 调用转换为 C# .NET
【发布时间】:2013-05-21 23:12:13
【问题描述】:

我有一个在本地服务器上完美运行的 AJAX 调用。我正在尝试通过 C#(来自另一个应用程序/域)进行相同的调用,但我很困惑。

这是我的网络方法:

[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: " + "'myemail@hotmail.com'" + ", 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);
    }
});
}

重要提示:

我想要这个的主要原因是因为我想使用来自另一个域的 ParentLogin 方法。可能来自 iOS 或 Android 应用程序!这是正确的方法吗?

谢谢!

【问题讨论】:

  • 这是一个网站,还是一个网络应用项目?
  • 能否发布 ExecuteAjaxCall 的代码,或者为我们确定它所在的 JS 库,如果您使用的是 JQuery 等现成的框架?
  • John Saunders:这是一个 Web 应用程序。

标签: c# asp.net .net ajax rest


【解决方案1】:

此 Web 方法(实际上是“页面方法”)是 Web 应用程序中的公共静态方法。你应该可以直接调用它:

namespace.Login.ParentLogin(email, password);

【讨论】:

  • 谢谢。我添加了有关我的问题的更多信息。我需要能够从另一个域/应用程序/平台调用此方法。
【解决方案2】:

我会重构一个服务类来保存你的应用程序逻辑,这样你就可以在另一个项目中使用它而不是 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();
}

【讨论】:

  • 如果你的项目很简单,那么这有点过分了,但总的来说;我完全同意,这是我解决这个问题的首选方法。 +1 伟大的思想家:)
  • 谢谢!我添加了有关我的问题的更多信息。我需要能够从另一个域/应用程序/平台调用此方法。
  • 如果你想使用 C# 跨平台,你应该考虑使用Xamarin。如果你想使用本地语言,那么你需要像你正在做的那样制作一个 API。
猜你喜欢
  • 2020-07-21
  • 2015-07-25
  • 2019-03-06
  • 2011-06-26
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 2011-08-19
相关资源
最近更新 更多