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. RubyMine安装、破解

    经常安装东西,这是我安装过最快的ide破解版. 下载地址: http://www.jetbrains.com/ruby/download/index.html 破解序列号: name: rubymin ...

  2. Java 三大特性:封装、继承、多态

    一.封装: 对内 可封装自己的实现细节,使之信息隐藏不被其它类破坏: 对外 各个类之间分工明确, 其它类无需关心实现细节,类中细节发生改变,其它类也无需作出更改: 二.继承: 从已有的类中派生出新的类 ...

  3. Android源代码下载过程中无法下载repo的解决方法【转】

    本文转载自:http://blog.csdn.net/shangyuan21/article/details/17618575 我们都知道下载Android源代码需要使用repo进行辅助下载,但是最进 ...

  4. HDU 1022 之 Train Problem I

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. 织梦dedecms内页分类频道友情链接实现方法

    本文介绍了织梦dedecms中内页分类频道加友情链接的三种方法,有需要的朋友参考下. 织梦dedecms中内页分类频道加友情链接,方法有三种: 先讲方法,后讲原理: 方法:先找到首页模版index.h ...

  6. Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[])

    最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回转换,现在分享出来 如果想要请教Socket的问题请留言,我会随时回答的 1. int类型转16进制hexstring ...

  7. 让振动器振动起来——Vibrator的使用

    AndroidManifest.xml 获取系统权限 <uses-permission android:name="android.permission.VIBRATE"/& ...

  8. 深入理解WeakHashmap

    转自:http://mikewang.blog.51cto.com/3826268/880775 (一) 查看API文档,WeakHashmap要点如下: 1. 以弱键 实现的基于哈希表的 Map.在 ...

  9. VC++编译说明

    目录 第1章编译步骤    1 第2章编译源文件    2 2.1 编译器    2 2.2 包含头文件    3 2.3 重复包含    6 2.4 预编译头文件    7 2.4.1 创建     ...

  10. 【CQOI2009】中位数图

    [题目链接] 点击打开链接 [算法] 将小于m的数看作-1,大于m的看作1 然后求前缀和,如果区间[l,r]的中位数是m,显然有 : sum(r) - sum(l-1) = 0 因此,只需m的位置之前 ...