views:

29

answers:

2

Can I call a webservice from a Controller Action in ASP.Net MVC?

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
}

Basically I want to do this from my Action...

             $.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) {
                    }
                });
+1  A: 

If the web service is part of the same application you may not need to call it as a web service at all, you could just use it's classes as normal objects, methods, etc. ie just call the logic directly through code.

AUSteve
+1  A: 

In order to call a web service in C# you need to generate a client proxy from the WSDL. You need to add a service reference and use the generated proxy to consume the service.

Darin Dimitrov