2011-05-16 41 views
4

我在非MVC環境中使用Razor引擎(razorengine.codeplex.com)。我編譯存儲在文件中的模板,並使用@inherits來提供智能感知支持。RazorEngine - 命名空間導入編譯錯誤

  • RazorEngine大會
  • 自定義組件 - 引用RazorEngine,包含View<>並將View<>作爲基類
  • Web應用程序 - 引用RazorEngine,自定義程序集,包含.cshtml模板文件

所有CSHTML文件有以下指令@inherits指令:

@inherits View<SomeModel> 

將引發錯誤:

類型的命名空間的看法是沒有發現,是否缺少程序集引用?

我的web.config包含以下項:

<add namespace="CustomAssembly.NamespaceContainingViewClass" /> 

我覺得這是與其他入門<assemblies>,其中沒有提到我的CustomAssembly。是這樣嗎?我可以使用另一個程序集中包含的自定義基類進行編譯嗎?

p.s.我不能代表大會取得強大的名字,因爲我的自定義組件引用了3D方組件,其不強命名要麼...

堆棧跟蹤:

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

此錯誤在哪裏拋出?什麼是堆棧跟蹤? – SLaks 2011-05-16 12:54:17

+0

當我使用參數(其中一個是包含'@ inherits'的模板字符串)調用'Razor.Compile'時,會引發錯誤。我已經添加了堆棧跟蹤到上面的問題。 – Ropstah 2011-05-16 13:10:14

+0

在@inherits之後擁有完全限定的名稱時它工作嗎? MVC可能會做一些額外的工作來使命名空間起作用。你是否也嘗試使用@using MyNamespace在模板本身中添加命名空間; – marto 2011-05-16 13:23:54

回答

1

你可能需要添加刀片配置部分,將web.config

<?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> 
+2

我將它添加到我的文件夾包含的意見:'/佈局/ razor_views/web.config'沒有任何成功... – Ropstah 2011-05-17 07:29:41

+0

它應該在主'web.config'或' app.config' – Buildstarted 2011-05-17 13:12:40

+0

我仍然無法得到這個工作。 – 2011-12-30 20:49:51

5

您可以在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(); 
    }