Razor URL.Content抛出错误

时间:2015-10-09 18:55:06

标签: .net asp.net-mvc razorengine

我尝试使用剃须刀将图片添加到我的电子邮件中。使用我的app_data文件夹中的代码

<img src="@Url.Content("~/app_data/picture.png")" />

但Razor不断抛出这个错误:

    Errors while compiling a Template.
    Please try the following to solve the situation:
    * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    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' to make the model dynamic).
    NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
***More details about the error:
 - error: (29, 80) The name 'Url' does not exist in the current context
Temporary files of the compilation can be found in (please delete the folder)***

我想我某处错过了某个引用,但是我将它包括在哪里? 非常感谢任何帮助。

更新:我尝试将图片移动到内容文件夹并将代码行更改为

<img src="@Url.Content("~/Content/picture.png")" />

仍然没有运气。有什么新建议吗?

1 个答案:

答案 0 :(得分:0)

如果你想在电子邮件中放一张照片而你绝对需要将它们保存在app_data文件夹中(顺便说一句:这是一个不好的做法 - 它们应该在/ Content文件夹中),我建议你做一个控制器中的端点,根据请求返回图片:

public ActionMethod Pictures(string? pictureName)
{
    var path = "/App_Data/" + pictureName;
    if(File.exists(path))
    {
        return base.File(path, "image/jpeg");
    }
    else
    {
        //deal with invalid request
    }
}

这样,您可以保持愚蠢的内容安排,并将网址放入您的电子邮件中:

/Controller/Pictures/Image1.jpg