Sunday, 29 November 2015

Creating and consuming web service in ASP.NET MVC


  • First of all we'll create a Web Service and then we'll consume the web service using ASP.NET MVC application.
  • This project is created on Visual Studio 2013.
  • Open a VS and select ASP.NET Empty web application as shown.



























  • Then add new item and select Web Service (.asmx) extension would be there, rename if you want. Finally it would be some thing like this.






















  • Above you can see a [web method] , actually this allows a  methods to call remotely and access the data.
  • When you'll run the program you'll see HelloWorld method which is created by default. Now there after you'll invoke the HelloWorld which will return output as "Hello World".





























  • In a same way we'll create a Simple Web Method for addition of two numbers named Calc which will return a string. One can go through below code , we've created new method Calc which takes 3 inputs:
      1. query:  For Add, Multiply, etc 
      2. num1: number
      3. num2: number
  • Here it's only shown for Addition you can do as you want.





  • Now after creating the web method you'll see Calc method would be created. For testing your created service you can go to Calc and click. 









  • After click input Add and two numbers as shown and click on invoke. Be sure it should show result as in XML format and addition of two numbers.























  • Now we'll consume a Web Service using ASP.NET MVC application. So create an Empty ASP.NET MVC Application as shown below. 




























  • Before consuming Web Service above created project should be running or hosted on IIS to access.
  • Then add that url in this Address and Press Go. Your hosted or running Web Service would be seen in Service Panel as shown.








  • For accessing the method give a reference of Web Service in namespace as shown.









  •  Thereafter create an object as shown by SoapClient and then give input to Web Method by calling the service and you'll get the output.


























  • And that's it Over.  You'll get your data in result and can use in View of MVC application.

No comments:

Post a Comment

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....