In this article I will explain with an example, how to call (consume) Web Service (ASMX) in ASP.Net MVC Razor.
This article will illustrate how to call (consume) Web Service (ASMX) in ASP.Net MVC Razor by making call to a Public Web Service which returns the Country to which a supplied IP Address belongs.
 
Note: For illustration purpose, I am making use of the Free Web Service used for finding the geographical location of an IP Address.
 
 
Adding Reference of the Web Service in Visual Studio
1. Right click the project in Solution Explorer and choose Add Service Reference option from the context menu.
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
2. Now in the Add Service Reference Dialog you need to click on the Advanced button.
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
3. In the Service Reference Settings Dialog you need to click on Add Web Reference button.
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
4. Now in the Add Web Reference Dialog you need to add the URL of the Web Service and click the Green Go button to discover it. Once the Web Service is discovered, give its reference an appropriate name using the Web Reference Name TextBox and click on Add Reference button.
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
5. Finally you will notice the Web Service Reference has been added to your project.
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method gets called when the Form is posted.
An object of GeoIPService is created and then the value of the IP Address received from the Form is passed as parameter to the GetGeoIP method which returns the name of the Country to which the IP address belongs which is then displayed assigned to a ViewBag object.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(string iPAddress)
    {
        GeoService.GeoIPService service = new GeoService.GeoIPService();
        GeoService.GeoIP output = service.GetGeoIP(iPAddress.Trim());
        ViewBag.Country = "Country: " + output.CountryName;
 
        return View();
    }
}
 
 
View
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionNameName of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the Form, the TextBox and a Submit Button. When the Button is pressed, the Form is submitted and the fetched Country from the Web Service is displayed using a ViewBag object.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        @Html.TextBox("IPAddress")
        <input type="submit" value="Submit"/>
        <hr/>
        @ViewBag.Country
    }
</body>
</html>
 
 
Screenshot
Call (Consume) Web Service (ASMX) in ASP.Net MVC
 
 
Downloads