AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

ASP.NET MVC PartialView gives an error some times?

I use partialview like

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

And It is working on server now. But sometimes It gives error. Erros is below:

enter image description here

This error not occur allways.

I cant find any reason for this problem.And I cant understand why does it sometimes give this error.

If it is neccesary, I write the content of partialview and controller action.

action

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

I get the weather for specific location from google with this action. Thanks.

Upvotes: 1

Views: 417

Answers (2)

Gromer
Gromer

Reputation: 9931

Add a null reference check in your finally. Initializing GoogleResponse could fail, so it would still be null. Then you'll hit your finally block and get a null reference exception since GoogleResponse is null when you try to call .Close().

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

Upvotes: 1

ClearCrescendo
ClearCrescendo

Reputation: 1155

There is currently an intermittent 403 Forbidden response to the Google Weather API that you are using. See Google Weather API 403 Error

The reason for the intermittent 403 response is not known but has been a problem since the 7th of August 2012.

Upvotes: 2

Related Questions