1

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.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • 1
    It's likely that there is an error inside the HavaDurumuPartial-action. Sometimes the incorrect line-number is shown in errors. So check your Action and see if a NullReferenceException can be thrown. – Pbirkoff Aug 08 '12 at 15:24
  • The error is most likely occurring in the action method; can you post sample code from your controller? – sellmeadog Aug 08 '12 at 15:24
  • Sometimes Xml file can be null? I wonder, XML file come from google periodically? – AliRıza Adıyahşi Aug 08 '12 at 15:29
  • 1
    Just a minor (major?) point...you are mixing concerns (WRT your ViewBag stuff where are you are having your controller return HTML directly). This is decidedly non-MVC. I would highly recommend refactoring, return the data you want (via JSON or via a model) to your views and let the view build itself from there. – JasCav Aug 08 '12 at 15:38
  • @JasCav - I would disagree. Returning a PartialView is very much MVC, otherwise the Html.Action and PartialView ActionResults would not exist. I agree he's doing things in his action method he shouldn't, but returning HTML is not one of them. – Erik Funkenbusch Aug 08 '12 at 15:44
  • @MystereMan - I wasn't arguing the use of using Partial Views at all (use them all the time myself). What I don't believe is right (although, I'm open to being wrong) is mixing in the HTML directly into his controller. By doing that, he isn't separating concerns. If he ever wants to redesign his views, he is going to have to come in and change his controller. If he was just returning data as I suggested, he would only have to write the view to display the data. I don't believe the controller should not be building the view as he is doing now. – JasCav Aug 08 '12 at 15:54

2 Answers2

2

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.

Community
  • 1
  • 1
ClearCrescendo
  • 1,145
  • 11
  • 22
  • Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead. http://stackoverflow.com/a/11890885/419 – Kev Aug 09 '12 at 22:44
1

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();
    }
}
Gromer
  • 9,861
  • 4
  • 34
  • 55