我正在尝试使用MongoDB在Visual Studio 2013中设置ASP.NET MVC5项目。以下是我到目前为止所做的事情:
1)在Visual Studio中创建项目
2)下载了MongoDB.Bson.dll和MongoDB.Driver.dll
3)在我的Visual Studio项目中添加了对这两个dll中每个dll的引用
4)在HomeController.cs中编写以下代码:
namespace MyProject.Controllers
{
public class HomeController : Controller
{
private MongoClient _client;
private MongoServer _server;
private MongoDatabase _database;
public HomeController()
{
_client = new MongoClient(ConfigurationManager.ConnectionStrings["Mongo"].ConnectionString);
_server = _client.GetServer();
_database = _server.GetDatabase(ConfigurationManager.AppSettings["DbName"]);
}
public ActionResult Index()
{
return View();
}
}
}
但是,当我运行Visual Studio调试器时,我在显示_client = new MongoClient(.....)
的行上收到以下消息:
NullReferenceException was unhandled by user code
An exception of type 'System.NullReferenceException' occurred in MyProject.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
我错过了重要的事情吗?感谢。
答案 0 :(得分:0)
您的配置文件中似乎没有名为Mongo的连接字符串。
仔细检查您是否有以下内容:
<add name="Mongo" connectionString="mongodb://localhost/yourDatabase" />