网页API 2控制器下载维基百科的API和显示Web的JSON输出 [英] Web Api 2 controller to download wikipedia api and show json output on web
问题描述
我试图解析维基百科的API,它包含article.I的短文本正在使用ASP.Net MVC编码。我的维基百科API是<一个href=\"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Berlin&redirects=\" rel=\"nofollow\">https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Berlin&redirects=这是格式化的JSON。在present我所做的是 - 在模型中我创建了一个名为维基文件夹,里面我创建了一个名为Limits.cs,Pageval.cs,Query.cs,四级Rootobject.cs
公共类限制
{
公众诠释提取物{搞定;组; }
}公共类Pageval
{
公众诠释的pageid {搞定;组; }
公众诠释NS {搞定;组; }
公共字符串的标题{搞定;组; }
公共字符串提取物{搞定;组; }
}
公共类查询
{
公共字典&LT;字符串,Pageval&GT;页面{搞定;组; }
}公共类Rootobject
{
公共字符串batchcomplete {搞定;组; }
市民查询查询{搞定;组; }
公共限制限制{搞定;组; }
}
现在在控制类我创建了一个2的WebAPI到contrller使网络上的模型对象秀。在这种情况下,我,因为我在MVC是新处理这种情况很新。我想在这种方式来分析,但它不能在所有工作
公共类WikiController:ApiController
{
// GET:API /维基
// GET:API /维基/ 5
公共字符串GetShortText(字符串名称)
{ 字符串结果; 使用(Web客户端的客户端=新的WebClient())
{ VAR响应= client.DownloadString(\"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=\" +姓名+&放大器;重定向=); 变种responseJson = JsonConvert.DeserializeObject&所述; Rootobject&GT;(响应);
VAR firstKey = responseJson.query.pages.First()键。
VAR提取物= responseJson.query.pages [firstKey] .extract; 尝试
{
正则表达式的正则表达式=新的正则表达式(@。?(小于= \\()[^()] *(= \\))()?);
的String.Format(之前:{0},提取物);
提取物= regex.Replace(提取,的String.Empty);
字符串RESULT1 =的String.Format(摘录);
结果= Regex.Replace(RESULT1,@\\\\ N,);
}
赶上(例外)
{
结果=错误;
}
} 返回结果;
}
该Routconfig是 -
公共类RouteConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}
}
您可以做几件事情。您可以使用属性的路由,或者您可以为您的自定义的方法的路线。它之所以没有在此刻映射是,你不必定义你的参数的路线。所以去的路线的方式,你可以定义
routes.MapRoute(
名称:维基
网址:API /维基/ getshorttext /名,
默认:新{控制器=维基,行动=GetShortText,名字= UrlParameter.Optional}
)
在一个侧面说明,你正在执行I / O密集型操作,我建议让您的行动异步
使用异步和等待
.NET的功能。这样,当您等待维基百科回应你会不会阻止任何线程。此外的HttpClient
提供 DownloadStringAsync
这是awaitable。看看异步并等待
I am trying to parse a wikipedia api which contain the short text of an article.I am using ASP.Net MVC for coding. My wikipedia api is https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Berlin&redirects= which is in json formatted. At present what I have done is - inside the Model I created a folder named Wiki, and inside it I created four 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 the controller class I created a WebApi 2 contrller to make the model object show on the web. In this case I am very new in handling this situation because I am new at MVC. I am trying to parse in this way but it is not working at 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 do a couple of things. You can use attribute routing or you can define a route for your custom method. The reason it does not map at the moment is that you do not have a route that defines your parameter. So going the route way you can define
routes.MapRoute(
name: "Wiki",
url: "api/wiki/getshorttext/name",
defaults: new { controller = "Wiki", action = "GetShortText", name = UrlParameter.Optional }
)
;
On a side note as you are performing a I/O bound operation I would suggest making your action async
using async and await
feature of .net. This way you won't block any thread while you waiting for Wikipedia to respond. Also HttpClient
offers a DownloadStringAsync
which is awaitable. Have a look at async and await
这篇关于网页API 2控制器下载维基百科的API和显示Web的JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!