通過一個天氣預報案例學習asp.net WebService(基於asp.net MVC)

來源:互聯網
上載者:User

本文WebService資料來源:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

中國氣象局 http://www.cma.gov.cn/

本文參考文章:http://zhangkui.blog.51cto.com/1796259/497324    zhangkui的部落格


開始本文:

1、建立asp.net MVC程式,作為調用webservice的用戶端(使用webForm 、winForm同樣道理);

2、添加一個控制器,命名為GetWeather


3、添加“服務引用”,給找到的服務設定一個命名空間,我這裡用的是ServiceReferenceWeather


4、以上完成後可以看到


5、開啟控制器,添加以下代碼

        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、為GetWeather控制器添加一個View,我這裡用的Razor模板引擎,用aspx也是一樣的


7、將view的預設代碼替換為以下代碼(其實就是一些html和css,你完全可以自己寫)

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>GetWeather</title>    <style>        div div {            clear: both;            width: 80px;            height: 35px;            line-height: 35px;            color: #b200ff;            font-family: 'Microsoft YaHei';            text-align: right;            float: left;        }        .divContainer {            width: 600px;            height: 35px;            line-height:35px;        }    </style></head><body>    @using (Html.BeginForm())    {        <span>城市名(支援地級市、直轄市,暫不支援縣級市,區等):</span> <input type="text" name="txtCityName" value=" " />        <input type="submit" name="name" value="確定" /><br />        if (ViewData.Model != null)        {        <div class="divContainer">            <div>城市名:</div>            <label>@ViewData.Model[0]</label><span>--</span><label>@ViewData.Model[1]</label>        </div>         <div class="divContainer">            <div>今日天氣:</div>            <label>@ViewData.Model[6]</label>              <img src='/Images/@ViewData.Model[8]' alt="Alternate Text" />            <img src='/Images/@ViewData.Model[9]' alt="Alternate Text" />                          <label>@ViewData.Model[5]</label>             <label>@ViewData.Model[7]</label>        </div>         <div class="divContainer">            <div>查詢時間:</div>            <label>@ViewData.Model[4]</label>        </div>        <div class="divContainer">            <div>明日預報:</div>            <label>@ViewData.Model[13]</label>              <img src='/Images/@ViewData.Model[15]' alt="Alternate Text" />            <img src='/Images/@ViewData.Model[16]' alt="Alternate Text" />                          <label>@ViewData.Model[12]</label>              <label>@ViewData.Model[14]</label>        </div>        }    }</body></html>

8、將asp.net MVC的預設路由改為適合本案例的Controller 和action



按F5,將程式運行起來,你可以看到:


輸入:比如 揚州

本文完


擷取更多資料和協助,可以訪問:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

該WebService返回的string[]數組包含以下資料,可以用索引訪問(如同本文中用索引訪問ViewData.Model ):


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.