我可以从ASP.Net MVC中的Controller Action调用Web服务吗?

时间:2010-10-27 04:00:59

标签: asp.net web-services asp.net-mvc-2

我可以从ASP.Net MVC中的Controller Action调用Web服务吗?

public ActionResult Index()
{
    PersonObject person = new Person("Sam");
    //Call a webservice which is located in the same app under /Services/General.asmx/WebMethod and pass it person
}

基本上我想从我的行动中做到这一点......

             $.ajax({
                    type: "POST",
                    url: "/Services/General.asmx/WebMethod",
                    data: JSON.stringify(DTOInternetPricing),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(res) {
                    },
                    error: function(res) {
                    }
                });

2 个答案:

答案 0 :(得分:2)

如果Web服务是同一个应用程序的一部分,您可能根本不需要将其称为Web服务,您可以将它的类用作普通对象,方法等,即只需通过代码直接调用逻辑。

答案 1 :(得分:2)

要在C#中调用Web服务,您需要从WSDL生成客户端代理。您需要add a service reference并使用生成的代理来使用该服务。

相关问题