public ActionResult Index(string city)
{ if (string.IsNullOrEmpty(city))
{
city = "上海";
}
//ak需要自己到百度地图官网申请
Uri weatherInfo = new Uri("http://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=UKMGHnstHCOFzYBe2h70gi5fLsc0C0dG"); HttpWebRequest request = WebRequest.Create(weatherInfo) as HttpWebRequest;
List<WeatherData> weatherDataList = new List<WeatherData>(); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
string json = reader.ReadToEnd(); Weather model = JsonHelper.JsonToEntity<Weather>(json);
List<WeatherDto> weatherDtoList = new List<WeatherDto>();
List<Index> indexList = new List<Index>(); foreach (WeatherResult result in model.Results)
{
indexList = result.Index;
weatherDataList = result.Weather_Data;
}
for (int i = ; i < indexList.Count; i++)
{
WeatherDto weatherDto = new WeatherDto();
weatherDto.Des = indexList[i].Des;
weatherDto.Tipt = indexList[i].Tipt;
weatherDto.Title = indexList[i].Title;
weatherDto.Zs = indexList[i].Zs; if (i < weatherDataList.Count)
{
weatherDto.Date = weatherDataList[i].Date;
weatherDto.DayPictureUrl = weatherDataList[i].DayPictureUrl;
weatherDto.NightPictureUrl = weatherDataList[i].NightPictureUrl;
weatherDto.Temperature = weatherDataList[i].Temperature;
weatherDto.Weather = weatherDataList[i].Weather;
weatherDto.Wind = weatherDataList[i].Wind; }
weatherDtoList.Add(weatherDto);
}
} return View("NewView",weatherDataList);
} } public class WeatherDto
{
public string Title { get; set; } public string Zs { get; set; } public string Tipt { get; set; } public string Des { get; set; } public string Date { get; set; } public string DayPictureUrl { get; set; } public string NightPictureUrl { get; set; } public string Weather { get; set; } public string Wind { get; set; } public string Temperature { get; set; }
} public class Weather
{
public string Error { get; set; } public string Status { get; set; } public string Date { get; set; } public List<WeatherResult> Results { get; set; }
} public class WeatherResult
{
public string CurrentCity { get; set; } public string Pm25 { get; set; } public List<Index> Index { get; set; } public List<WeatherData> Weather_Data { get; set; }
}
 @model IEnumerable<MvcAjaxTest.Models.WeatherData>

 @{
Layout = null;
} <html>
<head>
<title>weather</title>
<link rel="shortcut icon" href="http://p8.qhimg.com/t0158c24c5ddb3a6745.png" type="image/x-icon">
<link rel="stylesheet" href="~/Content/css/Weather.css" />
<style type="text/css">
#weathertype {
font-size: 28px;
padding-left: 20px;
} #copyright {
padding: 40px ;
text-align: center;
line-height: ;
color: #;
} .btn-app {
background: url(http://p8.qhimg.com/t017399c8595fd6cc76.png) no-repeat 0 0;
} .skinmore, .skinmore2 {
z-index: -;
} .skin2 .skinmore2 {
background: url(http://p4.qhimg.com/t0191e46e3e10bc96e3.png) repeat center 0;
height: %;
left: ;
position: absolute;
top: ;
width: %;
} .qrcodes img {
margin-top: 40px;
} .qrcodes {
position: absolute;
top: 150px;
width: 200px;
right: %;
} .header {
height: 110px;
} .logo {
background: url(http://p9.qhimg.com/t01c2da4dacc6c6dee8.png) no-repeat;
background-image: -webkit-image-set(url(http://p9.qhimg.com/t01c2da4dacc6c6dee8.png) 1x,url(http://p0.qhimg.com/t01ea0b4aedd360d174.png) 2x);
float: left;
height: 65px;
margin-top: 35px;
width: 183px;
} .search {
margin-top: 20px;
}
</style>
<script type="text/javascript" src="~/Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
function Change() {
var citySelected = $("#GetCity option:selected").text();
$.ajax({
type: "Post",
url: "Weather/Index",
data: { City: citySelected },
success: function (data) {
$("#partRefresh").html(data);
},
error: function () {
alert("请求失败,请稍候再试...");
}
});
} </script> </head>
<body class="skin6" style="display: block;">
<div>
<select id="GetCity" onchange="Change()">
<option value="" selected="selected">上海市</option>
<option value="">合肥市</option>
<option value="">牡丹江市</option>
<option>湖州市</option>
<option>海口市</option>
</select>
</div>
<div class="morewether" id="partRefresh">
<!-- 各天气对应中文拼音 如icon-yewanqing对应“夜晚晴“ -->
<ul id="foreast">
<li>
<p class="colora">@Model.ToList()[].Date</p>
<p class="icon-tu"><i class="icon-qing"></i><br>@Model.ToList()[].Weather</p>
<p class="otherinfo"><span>@Model.ToList()[].Temperature</span>@Model.ToList()[].Wind</p>
</li>
<li>
<p class="colora">@Model.ToList()[].Date</p>
<p class="icon-tu"><i class="icon-shachen"></i><br>@Model.ToList()[].Weather</p>
<p class="otherinfo"><span>@Model.ToList()[].Temperature</span>@Model.ToList()[].Wind</p>
</li>
<li>
<p class="colora">@Model.ToList()[].Date</p>
<p class="icon-tu"><i class="icon-shachen"></i><br>@Model.ToList()[].Weather</p>
<p class="otherinfo"><span>@Model.ToList()[].Temperature</span>@Model.ToList()[].Wind</p>
</li>
<li>
<p class="colora">@Model.ToList()[].Date</p>
<p class="icon-tu"><i class="icon-shachen"></i><br>@Model.ToList()[].Weather</p>
<p class="otherinfo"><span>@Model.ToList()[].Temperature</span>@Model.ToList()[].Wind</p>
</li>
</ul>
</div>
</body>
</html>

效果图:

asp.net MVC中实现调取web api的更多相关文章

  1. ASP.NET MVC 提供与访问 Web Api

    ASP.NET MVC 提供与访问 Web Api 一.提供一个 Web Api 新建一个项目,类型就选 "Web Api".我用的是MVC5,结果生成的项目一大堆东西,还编译不过 ...

  2. 在ASP.NET Core MVC中构建简单 Web Api

    Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 ...

  3. ASP.NET MVC 4 (十二) Web API

    Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...

  4. ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API

    原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...

  5. 如何在 ASP.NET MVC 中集成 AngularJS(3)

    今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...

  6. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  7. 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)

    在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...

  8. 如何在 ASP.NET MVC 中集成 AngularJS(1)

    介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...

  9. ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器

    ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器一:闲谈一下:1.现在任务跟踪管理系统已经开发快要结束了,抽一点时间来写一下,想一想自己就有成就感啊!!  ...

随机推荐

  1. mysql---列的选取原则

    列选择原则: :字段类型优先级 整型 > date,整型>浮点型,time > enum,char>varchar > blob 列的特点分析: 整型: 定长,没有国家/ ...

  2. Linux 进程等待队列【转】

    本文转载自:http://blog.csdn.net/dlutbrucezhang/article/details/9212067 Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制 ...

  3. MYSQL初级学习笔记一:MYSQL常用命令和数据库操作(DDL)!(视频序号:初级_3,4)

    知识点一:MYSQL常用命令(3) 登入方法:一,mysql –u 账号 –p 密码 退出方法:一,EXIT,QUIT 修改MYSQL命令提示符: 连接上客户机之后,通常使用prompt命令修改: 连 ...

  4. 安装程序工具 (Installutil.exe)

    网址:https://msdn.microsoft.com/zh-cn/library/50614e95(VS.80).aspx  安装程序工具 (Installutil.exe) .NET Fram ...

  5. hadoop-3.0.0 配置中的 yarn.nodemanager.aux-services 项

    在hadoop-3.0.0-alpha4 的配置中,yarn.nodemanager.aux-services项的默认值是“mapreduce.shuffle”,但如果在hadoop-2.2 中继续使 ...

  6. hdu(1007) 最近点对 分治法

    最近点对一般想到枚举  ,一一枚举时间复杂度为n^2:枚举时候一些操作是多余的,有了分治算法的思想 ,把一些问题分个击破,再回到整体. 题目链接 以这道题为例,我们可以把他按照x轴的升序分成多个子区域 ...

  7. Intel® Media Server Studio Support

    复制自网址:https://software.intel.com/en-us/intel-media-server-studio-support/code-samples Code Samples M ...

  8. LA-3905 (扫描线)

    题意: 给一些流星的初始位置和运动向量,给了相机的拍摄范围;问你最多能拍到多少颗流星; 思路: 将流星用出现在相机拍摄范围内的时间段表示;sort后在扫面端点更新最大值; Ac代码: #include ...

  9. hdu-5675 ztr loves math(数学)

    题目链接: ztr loves math  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Othe ...

  10. Balancing Act(树的重心)

    传送门 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14070   Accepted: 593 ...