asp.net mvc - Web Api 2 controller to download wikipedia api and show json output on web -


i trying parse wikipedia api contain short text of article.i using asp.net mvc coding. wikipedia api https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=berlin&redirects= in json formatted. @ present have done - inside model created folder named wiki, , inside created 4 class named limits.cs, pageval.cs, query.cs, rootobject.cs.

public class limits {     public int extracts { get; set; } }  public class pageval {     public int pageid { get; set; }     public int ns { get; set; }     public string title { get; set; }     public string extract { get; set; } }  public class query {     public dictionary<string, pageval> pages { get; set; } }  public class rootobject {     public string batchcomplete { get; set; }     public query query { get; set; }     public limits limits { get; set; } } 

now in controller class created webapi 2 contrller make model object show on web. in case new in handling situation because new @ mvc. trying parse in way not working @ all.

 public class wikicontroller : apicontroller   {     // get: api/wiki       // get: api/wiki/5     public string getshorttext(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;     } 

the routconfig is-

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 }         );     } } 

you can couple of things. can use attribute routing or can define route custom method. reason not map @ moment not have route defines parameter. going route way can define

routes.maproute(             name: "wiki",             url: "api/wiki/getshorttext/name",             defaults: new { controller = "wiki", action = "getshorttext", name = urlparameter.optional }         ) 

;

on side note performing i/o bound operation suggest making action async using async , await feature of .net. way won't block thread while waiting wikipedia respond. httpclient offers downloadstringasync awaitable. have @ async , await


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -