智能感知无法在具有 razorengine 引用以获取模型属性的 dll 中的 cshtml 模板中工作

intellisense not working in a cshtml template in a dll that has razorengine reference to get a model properties

你好阅读了一些关于 razorengine 的文章,我也实现了同样的。

https://antaris.github.io/RazorEngine/IntellisenseAndResharper.html 但它对我不起作用。

甚至研究了 Whosebug 的代码并尝试解决它,但它也没有用。

注意:答案是 Whosebug 对我不起作用

这就是我尝试将智能感知添加到 cshtml 的方式

@using RazorEngine
@using Text.Model
@inherits Templating.TemplateBase<EmailModel>

我在名为 EmailModel 的文件夹模型下有一个模型,我在名为 EmailTemplate

的模板下有一个 HTML 模板

我的模型class

public class EmailModel
{
    public string Timestamp { get; set; }
    public string Name { get; set; }
}

我的目标是用 cshtml 编写

@using RazorEngine
@using Text.Model<!-- namespace-->
@inherits Templating.TemplateBase<EmailModel>

<html>
 <head></head>
  <body>
   <p>
     @Model.Name
     @Model.Timestamp
   </p>
  </body>
</html>

但我的智能感知仍然向我显示波浪线@Model

but still my intellisense is showing me squiggly lines @Model

不应该是因为您指的是 @Model,如下面的代码段所示,但我在您的 *.cshtml 中看不到任何地方,您绑定了 @Model .基本上,您的视图不是 强类型视图 。所以没有模型,因此没有错误

   <p>
     @Model.Name
     @Model.Timestamp
   </p>

@inherits 用于继承基础 class 作为您评论中的链接文档说

you can get minimal intellisense by providing your own base class and using it with the @inherits directive.

您需要添加 @Model 指令来绑定模型

@model namespace.modelname
<html>
 <head></head>
  <body>
   <p>
     @Model.Name
     @Model.Timestamp
   </p>
  </body>
</html>

有关详细信息,请参阅 Accessing Your Model's Data from a Controller

这是一个 dll 项目,所以我们不能使用@model 来绑定和获取 cs 中的智能感知帮助html。

以两种方式解决这个问题

1) 写一个class并在其中定义一个常量class

const string EmailTemplate =@"<html>
                                 <p>
                                   @model.Name is going to @model.Country
                                  </p>
                               </html>";

将此常量调用到您调用 razorengine

的 class 中

并将此 EmailTemplate 作为 razorengine 的模板传递,而您将 EmailModel 作为模型

2)在argumnets部分

下写入App.config
<arguments>
    <add name="EmailBody" value="&lt;html&gt;
                                     &lt;body&gt;
                                         &lt;p&gt;
                                             @Model.Name is going to  
                                             @Model.Country*********   
                                         &lt;/p&gt;
                                     &lt;/body&gt;
                                 &lt;/html&gt;" />
<arguments/>

注意:html 常规 <> 标签在 app.config

中不起作用

这两种方法正在使 razorengine 在非 MVC(dll 和控制台)应用程序中创建电子邮件模板