Webservic invoking the Weather forecast service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.