First, create a service link by right-clicking on the project name in the solution explorer, then hover over the "Add" parameter and click on "Service Link ..."
Secondly, paste the web service address in the "Address" field on the "Add a service link" page, be sure to add "? Wsdl" to the end of your web service or it will not work, and then click "Go". You will see that the web service is displayed in the Services area. Click on a service to view the available services that appear in the Operations section. Rename the service if you want, then click OK to create the service.
Finally, put the following code in your MVC controller. Put the code in the Get or Post controller, it doesn't matter.
// Declare the Request object. ServiceReference1.GetSurveyRequest myGSRq = new ServiceReference1.GetSurveyRequest(); // You can set the webservice parameters here like... myGSRq.Address = getUserData[0].address; myGSRq.City = getUserData[0].city; myGSRq.State = getUserData[0].state; // Now declare the Response object. ServiceReference1.GetSurveyResponse myGSR = new ServiceReference1.GetSurveyResponse(); //And then use the following to process the request and response. ServiceReference1.EMPortTypeClient emptc = new ServiceReference1.EMPortTypeClient(); myGSR = emptc.GetSurvey(myGSRq); // In this example the response Object comes back with a URL used to redirect the user //after the webservice has been processed. if (myGSR.Url != null) Response.Redirect(myGSR.Url.ToString()); else Response.Write("Error"); return null;
Pretty simple, hope this helps!
If you are creating a new service and are able to use a web service or a web API, I suggest using a web API. Build a RESTful API using ASP.NET Web APIs
source share