示例#1
0
        public async Task <ActionResult> Index(string city, int days = 1)
        {
            ViewBag.Cities = _cities;
            if (string.IsNullOrEmpty(city))
            {
                return(View());
            }
            try
            {
                ForecastObject forecast = await _forecastProvider.GetForecastAsync(city, days);

                ForecastEntity entity = _converter.ConvertToEntity(forecast);
                await _weatherService.AddForecastAsync(entity);

                return(View(forecast));
            }
            catch (WebException ex)
            {
                ViewBag.ErrorMessage = $"Weather forecast for city '{city}' not found.";
                return(View());
            }
        }
示例#2
0
        public async Task <HttpResponseMessage> ShowForecast(string city, int days)
        {
            if (string.IsNullOrEmpty(city))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
            try
            {
                var forecast = await _forecastProvider.GetForecastAsync(city, days);

                var forecastEntity = _forecastConverter.ConvertToEntity(forecast);
                await _weatherService.AddForecastAsync(forecastEntity);

                return(Request.CreateResponse <ForecastObject>(HttpStatusCode.OK, forecast));
            }
            catch (ArgumentException)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, InvalidDays));
            }
            catch (WebException)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, string.Format(InvalidCity, city)));
            }
        }