2015-02-02 5 views
1

에서 최신 버전의 면도기를 사용하고 있습니다. https://github.com/Antaris/RazorEngine면도기 런 컴 파일이 디버그를 허용하지 않습니다

일부 cshtml에 첨부하고 디버깅하고 싶습니다. 내 코드에서

Debugging 

    One thing you might want to enable is the debugging feature: 

    config.Debug = true; 

    When Debug is true you can straight up debug into the generated code. RazorEngine also supports debugging directly into the template files (normally .cshtml files). As as you might see in the above code there is no file to debug into. To provide RazorEngine with the necessary information you need to tell where the file can be found: 

    string template = "Hello @Model.Name, welcome to RazorEngine!"; 
    string templateFile = "C:/mytemplate.cshtml" 
    var result = 
     Engine.Razor.RunCompile(new LoadedTemplateSource(template, templateFile), "templateKey", null, new { 

다음

Readme 파일 상태는 내가 설정을 가지고있는

var config = new TemplateServiceConfiguration(); 
    // .. configure your instance 
    config.Debug = true; 

    var service = RazorEngineService.Create(config); 

    Engine.Razor = service; 

    //string template = "Hello @Model.Name, welcome to RazorEngine!"; 
    string templateFile = "C:/mytemplate.cshtml"; 

    var template = new LoadedTemplateSource("", templateFile); 
    var result = 
     Engine.Razor.RunCompile(template, this.Name, null, model); 

지금 나는 그것에서 다음과 같이 해당 경로에서 cshtml 파일을 만든 다음. 내가 그것으로 F11 때

@{ 

     var a = 1; 

     a = a + a; 

     @a 

    } 

    <div> 
     hi 
    </div> 

하지만 빈 문자열 :( 을 반환받을 그리고 그것은 그냥 단계 :(:(. 어떤 아이디어를 가지고 무엇을 메신저 잘못된 사람을하고

임 확실하지.


응답 코드는

string templateFile = "C:/mytemplate.cshtml"; 
    StringBuilder sb = new StringBuilder(); 
    using (StreamReader sr = new StreamReader(templateFile)) 
    { 
     String line; 
     // Read and display lines from the file until the end of 
     // the file is reached. 
     while ((line = sr.ReadLine()) != null) 
     { 
      sb.AppendLine(line); 
     } 
    } 
    string allines = sb.ToString(); 

    var template = new LoadedTemplateSource(allines, templateFile); 
    var result = 
     Engine.Razor.RunCompile(template, this.Name, null, model); 
+1

,하지만 당신은'File.ReadAllText (templateFile)와 쉽게 파일의 내용을 읽을 수는' –

+0

하, 즉 그것을하는 더 나은 방법입니다. 왜 내가 그렇게했는지 모르겠다. S. – Spaceman

답변

5

LoadedTemplateSource는 템플릿 소스 공동 대표 de, 그리고 소스 코드로 ""주어 졌으므로 템플릿이 비어 있습니다.

LoadedTemplateSource의 첫 번째 매개 변수는 템플릿의 소스 코드 여야하며 두 번째 매개 변수는 파일 경로이며, 디버깅 목적으로 만 사용됩니다.

게으른 로딩 또는 사용자 지정 로더 전략이 필요한 경우 사용자 지정 ITemplateSource 또는 ITemplateManager를 구현할 수 있지만 소스에서 메모리를 사용할 수있게되면 모든 오류 메시지가 향상됩니다.


matthid하는 RazorEngine의 기여자 주제 오프

+0

감사합니다 ... 왜 LoadedTemplateSource가 두 개의 매개 변수를 필요로하는지 명확하지 않았습니다. –

+0

그래서 파일을 열어 내용을 읽고 경로와 내용을 동시에 전달해야합니까? – Spaceman

+0

대단한 성공 : D – Spaceman