asp.net MVC中实现调取web api
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的更多相关文章
- ASP.NET MVC 提供与访问 Web Api
ASP.NET MVC 提供与访问 Web Api 一.提供一个 Web Api 新建一个项目,类型就选 "Web Api".我用的是MVC5,结果生成的项目一大堆东西,还编译不过 ...
- 在ASP.NET Core MVC中构建简单 Web Api
Getting Started 在 ASP.NET Core MVC 框架中,ASP.NET 团队为我们提供了一整套的用于构建一个 Web 中的各种部分所需的套件,那么有些时候我们只需要做一个简单的 ...
- ASP.NET MVC 4 (十二) Web API
Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...
- 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 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(3)
今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)
在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(1)
介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...
- ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器
ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器一:闲谈一下:1.现在任务跟踪管理系统已经开发快要结束了,抽一点时间来写一下,想一想自己就有成就感啊!! ...
随机推荐
- x$kccle视图深入剖析
今天是2014-05-27,实在无聊顺便研究一下x$kccle的内容吧.例如以下所有是自己分析和实验结果,真实可靠. 1.怎样获得v$log的底层表?我们能够通过autotrace完毕查看如 ...
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml
由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用. 那么得在现有Service项目中添加Http请求方式来实现系统间数据交互.只需要告知请求地 ...
- JAVA 中 synchronized 详解
看到一篇关于JAVA中synchronized的用法的详解,觉得不错遂转载之..... 原文地址: http://www.cnblogs.com/GnagWang/archive/2011/02/27 ...
- 【旧文章搬运】PspCidTable概述
原文发表于百度空间,2009-03-28========================================================================== PspCi ...
- Linux限制端口
设置防火墙 iptables -a input -p 协议 -s 可以访问ip -dport端口 -j ACCEPT
- 如何 Xcode 开发工具里安装一个空的项目末模板
很多朋友因为Xcode升级取消了空工程模板而发愁 今天给大家推荐一个简单方便的方法,导入空工程模板 对于 xcode7 来说可以使用下面的方法添加空模板.建议在升级的时候,不要下载beta版,最好下 ...
- Centos添加jdk环境变量
假设将jdk解压到/opt/jdk1.8.0_131. echo "export JAVA_HOME=/opt/jdk1.8.0_131" >> /etc/profil ...
- NC文件的处理【netcdf】
NC是气象领域数据的标准格式之一. 能够更好的存储格点数据. 下面为测试NC文件的读写. git:https://git.oschina.net/ipnunu/nctest pom.xml <p ...
- UVa 1643 Angle and Squares (计算几何)
题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...