2016-07-13 2 views
0

내 MVC 프로젝트의보기 폴더에서 .cshtml 페이지를 사용할 수 있습니다. 내 레이아웃 페이지에는 검색 옵션이 있습니다. 따라서 누군가가 어떤 단어로 검색하면, 그 단어를 모두 .cshtml 페이지에서 검색하고 뷰 이름을 반환하고 싶습니다. 어떻게 이것을 MVC에서 얻을 수 있습니까?MVC에서 전체보기 폴더의 단어를 검색하는 방법

+1

당신은 같은 검색 인덱싱 엔진이 필요합니다 [루씬 .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/) 또는 많은 제 3 자 검색 색인 생성 중 하나 아피스. 검색을 수행하기 위해 MVC에 내장 된 것이 없습니다. 또한 대부분 * content *가 뷰 모델을 통해 뷰에 추가되므로 * 뷰 *를 검색하지 않으려 고합니다. 대신보기 모델에 넣는 * 내용 *을 색인화해야합니다. – 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

고마워, 내 문제가 해결되었습니다. –

관련 문제