Learn asp.net WebService (based on ASP.net MVC) through a weather case study

Source: Internet
Author: User

This article WebService data source: Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

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

Article reference article: http://zhangkui.blog.51cto.com/1796259/497324 Zhangkui's Blog


Start this article:

1, the creation of asp.net MVC procedures, as the call WebService client (using WebForm, WinForm the same reason);

2, add a controller, named GetWeather


3, add "service Reference", to find the service set a namespace, I used here is Servicereferenceweather


4, the above can be seen after completion


5, open the controller, 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, I use the Razor template engine, with ASPX is the same


7, the default view of the code to replace the following code (in fact, some HTML and CSS, you can write yourself)

@{Layout = null;} <! DOCTYPE html>  

8. Change the default route of ASP.net MVC to controller and action for this case



Press F5 to run the program and you can see:


Input: such as Yangzhou

The end of this article


For more data and help, you can access: Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

The string[] array returned by this webservice contains the following data, which can be accessed by index (as with the index access Viewdata.model in this article):


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.