2012-08-08 36 views
1

我用partialview像ASP.NET MVC PartialView有时会出错?

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

,现在是工作在服务器上。但有时它会给出错误。错误回报低于:

enter image description here

这个错误不会发生百达。

我无法找到这个问题的任何原因。我不明白为什么有时会给这个错误。

如果有必要,我写下partialview和controller动作的内容。

行动

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(); 
    } 

我得到了从谷歌具体位置与此动作天气。 谢谢。

+1

这很可能是HavaDurumuPartial动作中出现错误。有时不正确的行号显示为错误。所以检查你的Action,看看是否可以抛出NullReferenceException。 – Pbirkoff 2012-08-08 15:24:14

+0

错误很可能发生在操作方法中;你能从你的控制器发布示例代码吗? – sellmeadog 2012-08-08 15:24:37

+0

我加了行动方法。 – 2012-08-08 15:26:33

回答

1

在最后添加一个空引用检查。初始化GoogleResponse可能会失败,因此它仍然为空。然后,当你尝试调用.Close()时,你会终于阻止并获得一个空引用异常,因为GoogleResponse为null。

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

解决方法就是这样。感谢帮助。 – 2012-08-09 20:17:34

+0

不是问题,很高兴它解决了! – Gromer 2012-08-09 20:28:22

2

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

的原因间歇403响应尚不清楚,但已自2012年8月7日的一个问题

+0

在发布复制和粘贴样板/逐字回答多个问题时要小心,这些往往会被社区标记为“垃圾”。如果你这样做,那么它通常意味着问题是重复的,所以标记它们。 http://stackoverflow.com/a/11890885/419 – Kev 2012-08-09 22:44:08