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.

6
  • 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. Commented Aug 8, 2012 at 15:24
  • The error is most likely occurring in the action method; can you post sample code from your controller? Commented Aug 8, 2012 at 15:24
  • Sometimes Xml file can be null? I wonder, XML file come from google periodically? Commented Aug 8, 2012 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. Commented Aug 8, 2012 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. Commented Aug 8, 2012 at 15:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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. stackoverflow.com/a/11890885/419
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();
    }
}

1 Comment

Solution is this. Thanks for help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.