In project development, in addition to the release of WebService to provide customer calls, but also often need to call some customers or third-party webservice services, here is a demo to demonstrate the call of a third-party weather service.
1. Weather Forecast Service Interface Description:
Here's an example of http://www.webxml.com.cn/WebServices/WeatherWebService.asmx, the weather service.
2. New Empty MVC Project Mvcappservice
3. Introduce a Web service. Right-click on the project reference and select "Add Service Reference", such as:
4. In the Add Service Reference window that pops up, click the "Advanced" button, such as:
5. Click the "Add Web Reference" button in the pop-up form, such as:
6. Enter the Web service address and modify the Web reference name, such as:
7. Download the weather picture from Http://www.webxml.com.cn/images/weather.zip, then unzip the file, copy the folder weather to the Project content folder (empty item without content folder, create it yourself)
8. Add a home controller, and two GetWeather methods to implement the code.
PublicActionResult GetWeather () {returnView (); } [HttpPost] PublicActionResult GetWeather (stringCity ) {Weather.weatherwebservice Client=NewWeather.weatherwebservice (); varStrlist =Client.getweatherbycityname (city); if(strlist[8] =="") {viewbag.msg="temporarily do not support the city you inquire"; } Else{Viewbag.imgurl=@"/content/weather/"+ strlist[8]; Viewbag.general= strlist[1] +" "+ strlist[6]; Viewbag.actually= strlist[Ten]; } returnView (); }
9. Add the GetWeather view with the following code:
@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title>GetWeather</title>@using (Html.BeginForm ("GetWeather","Home", FormMethod.Post)) { <div> Enter the city you are querying: @Html. TextBox (" City") <input type="Submit"Value="Enquiry"/></div> <div>Weather Description: @ViewBag. General @if (!string. IsNullOrEmpty (Viewbag.imgurl as string)) { "@ViewBag. Imgurl"/> } <div> Weather Facts: @ViewBag .actually</div> <div> @ViewBag .msg</div> </div> }</body>10. The results of the operation are as follows:
Webservic invoking the Weather forecast service