asp.net - Web-api controller to extract data from Wikipedia api in json format and show the result on web -


in project using 3 wikipedia api in json formatted extract data it. first api contain short-text of wikipedia article of places means first paragraph of article. second api contain latitude , longitude of places. , third api got page-image url information of places. want implement code using web-api controller asp.net mvc. wikipedia-api shortext is- https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=berlin&redirects=

wikipedia api forlatitide , longitude is- https://en.wikipedia.org/w/api.php?action=query&format=json&prop=coordinates&titles=berlin

so far have done create 3 folders inside model named shorttext, image , geo accordingly.

inside shorttext folder created 4 classes contain json object , named these limits.cs, pageval.cs, query.cs , rootobject.cs

limits.cs

public class limits {  public int extracts { get; set; }  } 

pageval.cs

public class pageval {  public int pageid { get; set; }  public int ns { get; set; }  public string title { get; set; }  public string extract { get; set; } } 

query.cs

 public class query  {    public dictionary<string, pageval> pages { get; set; }  } 

rootobject.cs

 public class rootobject  {   public string batchcomplete { get; set; }   public query query { get; set; }   public limits limits { get; set; }  } 

for latidute , longitude created 4 classes inside geo folder inside model named coordinate.cs,geoquery.cs,georootobject.cs,pageid,

 public class coordinate {     public double lat { get; set; }     public double lon { get; set; }     public string primary { get; set; }     public string globe { get; set; } }  public class geoquery {     public dictionary<string, pageid> pages { get; set; } }  public class georootobject {     public string batchcomplete { get; set; }     public geoquery query { get; set; } }  public class pageid {     public int pageid { get; set; }     public int ns { get; set; }     public string title { get; set; }     public list<coordinate> coordinates { get; set; } } 

for pageimage created 4 classes inside image folder inside model named imgpageval.cs,imgquery.cs,imgrootobject.cs,thumbnail.cs,

 public class imgpageval  {     public int pageid { get; set; }     public int ns { get; set; }     public string title { get; set; }     public thumbnail thumbnail { get; set; }     public string pageimage { get; set; }  }   public class imgquery  {     public dictionary<string, imgpageval> pages { get; set; }  }    public class imgrootobject   {     public string batchcomplete { get; set; }     public imgquery query { get; set; }   }   public class thumbnail   {     public string source { get; set; }     public int width { get; set; }     public int height { get; set; }  } 

so inside controller created web-api 2 controller class want use writing code extract data-

here code not working @ all.it doesn't want see on web.i wrote code in following way. thing not corrext way data

 public class wikicontroller : apicontroller   {       // get: api/wiki      public string get(string name)     {          string result;          using (webclient client = new webclient())         {              var response = client.downloadstring("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + name + "&redirects=");              var responsejson = jsonconvert.deserializeobject<rootobject>(response);             var firstkey = responsejson.query.pages.first().key;             var extract = responsejson.query.pages[firstkey].extract;              try             {                 regex regex = new regex(@".(?<=\()[^()]*(?=\)).(.)");                 string.format("before:{0}", extract);                 extract = regex.replace(extract, string.empty);                 string result1 = string.format(extract);                 result = regex.replace(result1, @"\\n", " ");             }               catch (exception)             {                 result = "error";             }         }            return result;     } 

my routconfig , webapiconfig class as-

   public class routeconfig    {     public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "home", action = "index", id = urlparameter.optional }         );      }    } 

webapiconfig.cs is

   public static class webapiconfig     {     public static void register(httpconfiguration config)     {         // web api configuration , services          // web api routes         config.maphttpattributeroutes();          config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional }         );     } } 

i want result in following way in web-

{  "name": "burgtor",   "shorttext": "the burgtor, built 1444 in late gothic style, northern city gate of hanseatic lübeck....  "geocoordinates": {  "longitude": 10.6912,  "latitude": 53.8738  },   "images": [   "8ab1df99.jpg or //image url"  ] } 

i trying lot find solution result. new handing mvc web api controller. not getting correct way proceed.also trying hard way of processing url json web api through web api controller. sorry such long description.

where have try catch blocks, fill custom result class , return that.

something this:

public class geocoordinates  {     public double latitude { get; set; }     public double longitude { get; set; } }  public class locationresult {     public string name { get; set; }     public string shorttext { get; set; }     public geocoordinates geocoordinates { get; set; }     public list<string> images { get; set; }     } 

and in code fill locationresult , return serialized version of that.

var result = new locationresult(); result.shorttext = responsejson.query.pages[firstkey].extract; result.name = responsejson.query.pages[firstkey].title; //etc.  return jsonconvert.serializeobject(result); 

resulting in:

public string get(string id)         {             using (webclient client = new webclient())             {              var response = client.downloadstring("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + id + "&redirects=");              var responsejson = jsonconvert.deserializeobject<rootobject>(response);             var firstkey = responsejson.query.pages.first().key;              var result = new locationresult();             result.shorttext = responsejson.query.pages[firstkey].extract;             result.name = responsejson.query.pages[firstkey].title;             //etc.              return jsonconvert.serializeobject(result);         }     } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -