c# - 如何使用 RazorEngine 将 System.Text.RegularExpressions 添加到模板?

标签 c# regex razor namespaces razorengine

我正在使用 RazorEngine 呈现 HTML 电子邮件,并希望包含辅助函数。其中之一使用正则表达式:

// template.cshtml
@using System.Text.RegularExpressions

@functions {
  public string FixImageUrlParam(string url, int width, int height)
  {
    Regex widthParam = new Regex("w=[0-9]*");
    Regex heightParam = new Regex("h=[0-9]*");

    url = widthParam.Replace(url, $"w={width}");
    url = heightParam.Replace(url, $"h={height}");

    return url;
  }
}

这是我的配置/渲染逻辑。

// renderer.cs
public static string RenderTemplate(string template, string dataModel)
{
    TemplateServiceConfiguration config = new TemplateServiceConfiguration();
    config.Namespaces.Add("System.Text.RegularExpressions");
    Engine.Razor = RazorEngineService.Create(config); ;


    Engine.Razor.AddTemplate("template", File.ReadAllText("template.cshtml"));
    Engine.Razor.Compile("template", null);
    return = Engine.Razor.Run("template", null, JsonConvert.DeserializeObject<ExpandoObject>(File.ReadAllText("data.json")));
}

问题是当 RazorEngine 尝试渲染时,我的辅助函数会导致错误。我已将错误隔离到使用 Regex 命名空间的行。

Errors while compiling a Template.
Please try the following to solve the situation:  
  * If the problem is about missing references either try to load the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' or 'typeof(DynamicObject)' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic)!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - error: (862, 35) Unexpected character '$'
\t - error: (863, 36) Unexpected character '$'
Temporary files of the compilation can be found in (please delete the folder): C:\\Users\\anstackh\\AppData\\Local\\Temp\\RazorEngine_3gknk4fd.poe

最佳答案

您是否尝试过删除字符串插值?很可能,这就是错误的原因。

尝试将第一个片段更改为:

// template.cshtml
@using System.Text.RegularExpressions

@functions {
  public string FixImageUrlParam(string url, int width, int height)
  {
    Regex widthParam = new Regex("w=[0-9]*");
    Regex heightParam = new Regex("h=[0-9]*");

    url = widthParam.Replace(url, "w=" + width.ToString());
    url = heightParam.Replace(url, "h=" + height.ToString());

    return url;
  }
}

关于c# - 如何使用 RazorEngine 将 System.Text.RegularExpressions 添加到模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51564037/

相关文章:

javascript - 更新 Markdown 链接中的 href

javascript - 有人知道为什么 "x".split(/(x)/).length 在 IE 中返回 0 吗?

c# - 在C#中保存图像文件

c# - 出于好奇——有没有更好的方法来替换这个字符串?

javascript - 使用 js Regex 在 URL 中查找特定静态文件

asp.net-mvc - 如何为DisplayFor()创建MVC Razor模板

asp.net-mvc - ASP.NET MVC 3 Razor Nested foreach with if 语句

c# - 处理 @HTML.Label 的 lambda

c# - 当作为 ActionResult 返回时,MemoryStream 是否会自动处理掉?

c# - 如何限制我的sql数据库中的行数,然后在达到限制时覆盖行?