Bonjour,
afin d'en apprendre un peu plus sur les services REST je lis actuellement le tutorial suivant : http://www.microsoft.com/web/post/co...h-aspnet-razor
J'ai une erreur a l'execution de la page.
Voici le code Weather.cshtml (situe dans le dossier App_Code) :
et Default.cshtml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @functions{ private static string WeatherService = "http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?listLatLon={0}&format=24+hourly&numDays=1"; private static string ZipcodeService = "http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php?listZipCodeList={0}"; public class Temperature { public string Zip { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public int MaxTemp { get; set; } public int MinTemp { get; set; } public string Forecast { get; set; } } private static string GetCoordinates(string zipcode){ string serviceURL = string.Format(ZipcodeService, zipcode); string dwml = string.Empty; System.Net.WebClient webClient = new System.Net.WebClient(); dwml = webClient.DownloadString(serviceURL); System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(dwml); var coordinates = xdoc.Descendants("latLonList").FirstOrDefault(); if(coordinates == null) return string.Empty; else return coordinates.Value; } private static Temperature GetWeather(string zipcode){ string coordinates = GetCoordinates(zipcode); if(coordinates == string.Empty){ return new Temperature(); } string serviceURL = string.Format(WeatherService, coordinates); string dwml = string.Empty; System.Net.WebClient webClient = new System.Net.WebClient(); dwml = webClient.DownloadString(serviceURL); return XmlToTemperature(dwml, coordinates, zipcode); } private static Temperature XmlToTemperature(string xml, string coordinates, string zipcode){ System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(xml); var maxTemp = from t in xdoc.Descendants("temperature").Elements("value") where t.Parent.Attribute("type").Value == "maximum" select t; var minTemp = from t in xdoc.Descendants("temperature").Elements("value") where t.Parent.Attribute("type").Value == "minimum" select t; var max = maxTemp.FirstOrDefault().Value; var min = minTemp.FirstOrDefault().Value; string forecast = xdoc.Descendants("weather-conditions").Attributes("weather-summary").FirstOrDefault().Value; string[] lonlat = coordinates.Split(','); Temperature temp = new Temperature(); temp.MaxTemp = Convert.ToInt32(max); temp.MinTemp = Convert.ToInt32(min); temp.Zip = zipcode; temp.Latitude = lonlat[0]; temp.Longitude = lonlat[1]; temp.Forecast = forecast; return temp; } }
Le message d'erreur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 @{ var temp = Weather.GetWeather("98052"); } <!DOCTYPE html> <html lang="fr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta charset="utf-8" /> <title>Titre de mon site</title> <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> </head> <body> <ol> <li>Zip code: @temp.Zip</li> <li>High: @temp.MaxTemp</li> <li>Low: @temp.MinTemp</li> <li>Forecast: @temp.Forecast</li> <li>Longitude: @temp.Longitude</li> <li>Latitude: @temp.Latitude</li> </ol> </body> </html>
Ligne d'erreurCS1061: 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' ne contient pas une définition pour 'Elements' et aucune méthode d'extension 'Elements' acceptant un premier argument de type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' n'a été trouvée (une directive using ou une référence d'assembly est-elle manquante ?)
Quelqu'un peut-il m'aider? Je ne comprend pas comment arranger cela... J'ai pourtant suivi les indications du tuto...
Code : Sélectionner tout - Visualiser dans une fenêtre à part var maxTemp = from t in xdoc.Descendants("temperature").Elements("value")
Merci d'avance
Partager