How To Get The Weather Information From A Public Weather API Using MVC And Angular

First we need to add html code below .we have one text box and one button .once enter city name and press the button You will the get that corresponding city weather details.

First we need to add html code. Below, we have one text box and one button. Once you enter the city name and press the button you will the get that corresponding city's weather details. 

You have to use an angularjs application with MVC controller.

HTML Code

  1. <div class="col-md-4">  
  2.        <div class="whetherad">  
  3.            <b> City:</b>  <input type="text" ng-model="cityName" placeholder="Enter City Name" />  
  4.            <button ng-click="getWeatherInformation()">Get Details</button><br /><br />  
  5.            <b>CityName:</b>{{city}}<br /><br />  
  6.            <b>Temperature:</b>{{temprature}}<br /><br />  
  7.            <b>Clouds:</b>{{clouds}}<br /><br />  
  8.            <b>Weather:</b>{{weather}}<br /><br />  
  9.            <b>Humidity:</b>{{humidity}}<br /><br />  
  10.            <b>Wind:</b>{{wind}}<br /><br />  
  11.            <b>WindValue:</b>{{windValue}}<br /><br />  
  12.            <b>Speed:</b>{{speed}}  
  13.        </div>  
  14.    </div>  

You will get the JSON response from your controller. From that json you can extract the required information using AngularJS code below.

JS Code

  1. //get weather information  
  2. //  
  3. $scope.getWeatherInformation = function () {  
  4.   
  5.     if ($scope.cityName == undefined || $scope.cityName == null || $scope.cityName == "") {  
  6.         alert("Please enter city name");  
  7.         return false;  
  8.     }  
  9.   
  10.     $http.get("/Registration/GetWeatherReport?cityName=" + $scope.cityName).then(  
  11.   
  12.         function (response) {  
  13.             $scope.temprature = response.data.current.temperature.value;  
  14.             $scope.humidity = response.data.current.humidity.value;  
  15.             $scope.wind = response.data.current.wind.direction.name;  
  16.             $scope.windValue = response.data.current.wind.direction.value;  
  17.             $scope.speed = response.data.current.wind.speed.value;  
  18.             $scope.weather = response.data.current.weather.value;  
  19.             $scope.clouds = response.data.current.clouds.name;  
  20.             $scope.city = response.data.current.city.name;  
  21.         },  
  22.         function (error) {  
  23.   
  24.         })  
  25. }     

First you need to add an empty MVC5 controller named WeaterController.

In MVC Controller we have GetWeatherReport method. We need to pass cityName from AngularJs and also we require AppId. We need to register in open weather site https://openweathermap.org/api. Once you register you will get the AppId. We need to pass it as parameter to the public api.

We will get the xml response from weather api, then we need to deserialize into JSON. In that JSON you will get @ symbol in every key. If we return the same JSON to the front end you will not get the keys.T o get the keys we need to remove the @ symbol before sending a response to the client side.

MVC Code

  1. [HttpGet]  
  2. public string GetWeatherReport(string cityName)  
  3. {  
  4.     string json = string.Empty;  
  5.     string createRequest = "http://api.openweathermap.org/data/2.5/weather?q=" + cityName + "&mode=xml&units=metric&appid=8u4ccd72e55b5b394d982135ef711fg4";  
  6.   
  7.     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(createRequest);  
  8.     try  
  9.     {  
  10.         using (HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse() as HttpWebResponse)  
  11.         {  
  12.             StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());  
  13.             JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();  
  14.             jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;  
  15.             string data = streamReader.ReadToEnd();  
  16.   
  17.             XmlDocument doc = new XmlDocument();  
  18.             doc.LoadXml(data);  
  19.             json = JsonConvert.SerializeXmlNode(doc).Replace("@""");  
  20.         }  
  21.     }  
  22.   
  23.     catch (Exception ex)  
  24.     {  
  25.         throw;  
  26.     }  
  27.     return json;  
  28. }  

The final output is shown below.

OutPut

Output