문제

저는 Razor 엔진(razorengine.codeplex.com) MVC가 아닌 환경에서.파일에 저장된 템플릿을 컴파일하고 사용합니다. @inherits 인텔리센스 지원을 위해.

  • RazorEngine 어셈블리
  • 사용자 정의 어셈블리 - RazorEngine을 참조하고 다음을 포함합니다. View<> 그리고 세트 View<> 기본 클래스로
  • 웹 애플리케이션 - RazorEngine, Custom Assembly 참조, .cshtml 템플릿 파일 포함

모든 cshtml 파일에는 다음이 포함됩니다. @inherits 지령:

@inherits View<SomeModel>

오류가 발생합니다:

네임스페이스 View 유형을 찾을 수 없습니다. 어셈블리 참조가 누락되었습니까?

내 web.config에는 다음 항목이 포함되어 있습니다.

<add namespace="CustomAssembly.NamespaceContainingViewClass" />

내 생각엔 이것이 다른 항목과 관련이 있는 것 같아 <assemblies>, 내 CustomAssembly 언급되지 않습니다.이것이 사실입니까?다른 어셈블리에 포함된 사용자 정의 기본 클래스로 컴파일할 수 있습니까?

추신.내 사용자 지정 어셈블리가 강력한 이름이 아닌 타사 어셈블리를 참조하기 때문에 어셈블리에 대한 강력한 이름을 검색할 수 없습니다.

스택트레이스:

at RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(TypeContext context)
at RazorEngine.Templating.TemplateService.CreateTemplate(String template, Type modelType)
at RazorEngine.Templating.TemplateService.GetTemplate(String template, Type modelType, String name)
at RazorEngine.Templating.TemplateService.Compile(String template, Type modelType, String name)
at RazorEngine.Razor.Compile(String template, Type modelType, String name)
도움이 되었습니까?

해결책

에 대한 면도기 구성 섹션을 추가해야합니다.

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="razorEngine" type="RazorEngine.Configuration.RazorEngineConfigurationSection, RazorEngine" requirePermission="false" />
    </configSections>
</configuration>

<razorEngine>
    <namespaces>
        <add namespace="CustomAssembly.NamespaceContainingViewClass" />
    </namespaces>
</razorEngine>
.

다른 팁

templateServiceConfiguration에 네임 스페이스를 추가 할 수 있습니다.

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration();
    templateConfig.Namespaces.Add("MyNamespaceGoesHere"); 
    templateConfig.Resolver = new DelegateTemplateResolver(name =>
    {
       <My template resolve implementation>
    }
    Razor.SetTemplateService(new TemplateService(templateConfig));
    using (TextWriter tw = new StringWriter())
    {
      Razor.Resolve(viewName + ".cshtml", model).Run(new ExecuteContext(), tw);
      var emailHtmlBody = tw.ToString();
    }
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top