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.现在任务跟踪管理系统已经开发快要结束了,抽一点时间来写一下,想一想自己就有成就感啊!! ...
随机推荐
- FMDB 使用技巧
源链接: http://blog.csdn.net/iunion/article/details/7091744 - (BOOL) isTableOK:(NSString *)tableName{ ...
- IDEA中使用git详细步骤
1.idea中配置git 设置 版本控制 git 配置git的执行路径(git.ext) 2.把项目推送到远程仓库(码云项目管理) a.在码云创建一个项目 b.复制项目的URL c.找到要上传到码云管 ...
- React引入,运行
1.引入 <script src="https://cdn.bootcss.com/react/15.5.4/react.min.js"></script> ...
- 一步一步学Silverlight 2系列(29):使用Transform实现更炫的效果(上)
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- My Notes
1.类似于border.margin.padding的四个方向数值顺序为上右下左.2.属性z-index参数值越大,则被层叠在最上面.3.标签<a>和属性display:block和适合在 ...
- dba操作之archivelog清理
下面的命令用于校验归档日志的有效性,列出无效的归档日志,以及以何种方式清除归档日志,列出几种常用的: crosscheck archivelog all; ...
- BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数
BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行 ...
- 小K的农场(差分约束)
题目大意 n个点 m条描述 农场 a 比农场 b 至少多种植了 c 个单位的作物. 农场 a 比农场 b 至多多种植了 c 个单位的作物. 农场 a 与农场 b 种植的作物数一样多. 题解 差分约束裸 ...
- ASP.NET Core:WebAppCoreAngular
ylbtech-ASP.NET Core:WebAppCoreAngular 1.返回顶部 1. 2. 3. 4. 5. 6. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 ...
- Hello World 4 JQuery
Hello World 4 JQuery <html> <head> <script type="text/javascript" src=" ...