Learning Asp.net WebService (based on ASP.net MVC) through a weather forecast Case Study)

Source: Internet
Author: User

The data source of WebService: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

China Meteorological Administration http://www.cma.gov.cn/

This article reference article: http://zhangkui.blog.51cto.com/1796259/497324 zhangkui blog

Start this article:

1. Create an Asp.net MVC program as the client that calls WebService (using webform and winform in the same way );

2. Add a controller named getweather.

3. Add "service reference" and set a namespace for the service. Here I use servicereferenceweather.

4. After completing the preceding steps, you can see

5. Open the controller and add the following code:

        public ActionResult GetWeather()        {            return View();        }        [HttpPost]        public ActionResult GetWeather(FormCollection formCollection)        {            string cityName = formCollection["txtCityName"];            ServiceReferenceWeather.WeatherWebServiceSoapClient w = new WeatherWebServiceSoapClient("WeatherWebServiceSoap");            string[] res = new string[23];            res = w.getWeatherbyCityName(cityName);            ViewData.Model= res;            return View();        }

6. Add a view for the getweather controller. The Razor template engine I use here is the same as Aspx.

7. Replace the default view code with the following code (in fact, some HTML and CSS, you can write them by yourself)

@ {Layout = NULL ;}<! Doctype HTML> 

8. Change the default route of Asp.net MVC to the Controller and action suitable for this case.

Press F5 to run the program. You can see:

Input: for example, Yangzhou

End of this Article

For more data and help, visit: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

The string [] array returned by the WebService contains the following data, which can be accessed using indexes (as in this article, viewdata. model is accessed using indexes ):

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.