2016-07-13 9 views
0

私のMVCプロジェクトのビューフォルダには.cshtmlページが多数あります。私のレイアウトページには検索オプションが用意されているので、誰かが何か単語を検索すると、その単語をすべて.cshtmlページで検索し、ビュー名を返したいと思っています。 これをMVCでどのように達成できますか?MVCのビューフォルダ内の単語を検索するには

+1

次のような検索インデックスエンジンを必要とする[Luceneの.NET](https://www.nuget.org/packages/Lucene.Net/)または[ Elastic Search](https://damienbod.com/2014/10/01/full-text-search-with-asp-net-mvc-jquery-autocomplete-and-elasticsearch/)または多数の第三者検索インデックスのうちの1つAPI。検索を行うためにMVCに組み込まれているものは何もありません。また、*ほとんどの*コンテンツ*はビューモデルを介してビューに追加されるため、おそらく*ビュー*を検索する必要はありません。代わりにビュー・モデルに入れる*コンテンツ*を索引付けする必要があります。 – NightOwl888

+0

あなたは、私たちのテキスト全体を弾性検索やそこからの検索のようなデータベースにインデックスする必要があることを意味します。右? –

+0

はい。最高のパフォーマンスを得るには、検索時から帯域外インデックスを作成する必要があります。サイトを一度索引付けしてから何度も検索します。 – NightOwl888

答えて

1

これを行うための可能な方法:

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

ありがとう、これは私の問題を解決しました。 –

関連する問題