asp.net MVC中实现调取web api

时间:2023-03-09 19:27:35
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