ASP.NET MVC PartialView有时会出错?

时间:2012-08-08 15:14:13

标签: asp.net-mvc partial-views

我使用像

这样的partialview
<td style="vertical-align: top;">@Html.Action("_HavaDurumuPartial")

现在正在服务器上工作。但有时它会给出错误。 Erros如下:

enter image description here

此错误不会发生。

我无法找到这个问题的任何原因。我无法理解为什么它有时会出现这个错误。

如果是必要的,我会写出部分视图和控制器动作的内容。

动作

public ActionResult _HavaDurumuPartial(string il)
    {
        il = "Izmir";
        HttpWebRequest GoogleRequest;
        HttpWebResponse GoogleResponse = null;
        XmlDocument GoogleXMLdoc = null;
        try
        {
            GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + il + "&hl=tr&ie=utf-8&oe=utf-8");
            GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
            GoogleXMLdoc = new XmlDocument();
            GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
            XmlNode root = GoogleXMLdoc.DocumentElement;
            XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
            //ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<b>Şehir : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>";
            XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");

            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<table cellpadding=\"5\"><tbody><tr><td style=\"width:50%;\"><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b></br>";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<b>Şuan:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + " " + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "</br>" + "";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + " " + nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText;
            nodeList = root.SelectNodes("descendant::weather/forecast_conditions");
            int i = 0;
            foreach (XmlNode nod in nodeList)
            {
                if (i == 0)
                {
                    i++;
                    continue;
                }
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + "</td><td align=\"center\">" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "</br>" + "";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<img src=\"http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "\" alt=\"" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "\">" + "</br>";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + nod.SelectSingleNode("low").Attributes["data"].InnerText + "°C" + "</br>";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + nod.SelectSingleNode("high").Attributes["data"].InnerText + "°C" + "</br>";
            }
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "</td></tr></tbody></table>";
        }
        catch (System.Exception ex)
        {
            ViewBag.HavaDurumu = ex.Message;
        }
        finally
        {
            GoogleResponse.Close();
        }
        return PartialView();
    }

我通过此操作从谷歌获取特定位置的天气。 感谢。

2 个答案:

答案 0 :(得分:2)

您正在使用的Google Weather API目前有间歇性的403 Forbidden响应。见Google Weather API 403 Error

间歇性403响应的原因尚不清楚,但自2012年8月7日以来一直存在问题。

答案 1 :(得分:1)

在finally中添加空引用检查。初始化GoogleResponse可能会失败,因此它仍然为null。然后你会点击你的finally块并得到一个空引用异常,因为当你试图调用.Close()时,GoogleResponse为null。

finally
{
    if (GoogleResponse != null)
    {
        GoogleResponse.Close();
    }
}