Hangfire类型或命名空间找不到(你是否缺少using指令或程序集引用?)。

5
我将遵循Hangfire.io上的教程进行翻译:http://docs.hangfire.io/en/latest/tutorials/send-email.html 然而,当我复制并粘贴提供的~/Views/Emails/NewComment.cshtml代码时:
@model Hangfire.Mailer.Models.NewCommentEmail

To: @Model.To
From: mailer@example.com
Subject: New comment posted

Hello,
There is a new comment from @Model.UserName:

@Model.Comment

<3

我遇到了以下错误:
Error   2   The type or namespace name 'Hangfire' could not be found (are you missing a using directive or an assembly reference?)
Error   3   The name 'Model' does not exist in the current context  

Models/NewCommentEmail.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Postal;

namespace HangFire.Mailer.Models
{
    public class NewCommentEmail : Email
    {

        public string To { get; set; }
        public string UserName { get; set; }
        public string Comment { get; set; }
    }
}

当我创建其他类型的视图时,例如在Views/Email中使用"MVC 5 View Page (Razor)"或"MVC 5 View Page Layout (Razor)",页面上所有以"@"开头的元素都显示为无法在当前上下文中找到。该项目解决方案名称为"Hangfire.Mailer"。


你能展示一下 Hangfire.Mailer.Models.NewCommentEmail 类的定义吗? - jamesSampica
@jerryh91 这两个文件在同一个 DLL 中吗? - Omri Aharon
我们怎么知道?你在说哪个dll? - jerryh91
如果cs文件和cshtml文件位于解决方案中的不同项目中,则需要在包含cshtml文件的项目中添加引用。 - Omri Aharon
在命名空间中使用Hangfire可能不是一个好主意。虽然你正在使用它,但当你尝试引用命名空间时,很难确定该命名空间来自哪个程序集。通常,你的模型命名空间应该是类似于MyApplication.Models的东西,其中MyApplication被替换为你的应用程序名称(例如JerryBlog)。 - mason
显示剩余3条评论
3个回答

7

C#是大小写敏感的,因此您应该使用Hangfire或HangFire。

现在您在定义中使用大写字母:

namespace HangFire.Mailer.Models

但在引用中使用小写字母:
@model Hangfire.Mailer.Models.NewCommentEmail

您链接的页面在命名空间中使用小写字母:

namespace Hangfire.Mailer.Models

1

0

@Ulf的回答似乎是正确的,但你提到任何以@开头的代码也无法工作(如果我理解正确的话)。

你的Views文件夹中是否有web.config文件?你需要它来配置razor和其他东西。如果没有,请创建一个新项目并将生成的Views/web.config文件复制粘贴到你自己的文件夹中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接