[英]how to search any word in entire view folder in mvc
我的MVC項目的檢視資料夾中有許多.cshtml網頁。 在我的布局頁面中,有可用的搜索選項,因此當某人按任何單詞搜索時,我想在所有.cshtml頁面中搜索該單詞並返回視圖名稱。 我如何在MVC中實現這一目標?
一種可能的方法是:
string path = Server.MapPath("~/Views"); //path to start searching.
if (Directory.Exists(path))
{
ProcessDirectory(path);
}
//Loop through each file and directory of provided path.
public void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
string found = ProcessFile(fileName);
}
//Recursive loop through subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
ProcessDirectory(subdirectory);
}
}
//Get contents of file and search specified text.
public string ProcessFile(string filepath)
{
string content = string.Empty;
string strWordSearched = "test";
using (var stream = new StreamReader(filepath))
{
content = stream.ReadToEnd();
int index = content.IndexOf(strWordSearched);
if (index > -1)
{
return Path.GetFileName(filepath);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.