我有从 View 获取数据并用它进行搜索的 asp.net 核心控制器
这是控制器代码
private readonly GettingWords _repository;
public HomeController(GettingWords repository){
_repository = repository;
}
[HttpPost]
public JsonResult SearchWord([FromBody] RequestModel model){
var result = _repository.GettingWord(model.word, model.adress);
return Json(result);
}
这是它调用的方法
public class GettingWords
{
public string GettingWord(string word, string adress)
{
string result;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(adress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd();
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
response.Close();
readStream.Close();
return result;
}
}
我需要使用这两个参数在新线程中运行 GettingWord。我怎样才能正确地做到这一点?
更新
另外我需要设置最大线程数,所以我认为这Task<>不是很好
胡子哥哥
守着星空守着你
犯罪嫌疑人X
相关分类