简体   繁体   English

无法从ASP.Net应用程序连接到MongoDB

[英]Can't connect to MongoDB from an ASP.Net application

I downloaded the driver (version 2.4.4) and first I just want to connect and read some existing data. 我下载了驱动程序(版本2.4.4),首先我只想连接并读取一些现有数据。

So I tried everything I found online. 因此,我尝试了所有在网上找到的内容。 Here's my code: 这是我的代码:

MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(connectionString, 27017);
var client = new MongoClient(settings);

my connectionString looks like this: 我的connectionString看起来像这样:

mongodb://x.x.x.x

and the error I'm getting is: 我得到的错误是:

 An exception of type 'System.ArgumentException' occurred in 
 MongoDB.Driver.Core.dll but was not handled in user code

 Additional information: 'mongodb://x.x.x.x:27017' is not a valid end point.

I couldn't find this error anywhere online. 我在网上任何地方都找不到此错误。 Please help :) 请帮忙 :)

edit: I'm not using localhost and am running a real IP. 编辑:我不使用本地主机,并且正在运行一个真正的IP。 the xxxx is just for the question purpose. xxxx仅用于提问目的。

Well xxxx is not a valid address. 那么xxxx不是有效地址。 If you're running mongodb locally you can use: 如果您在本地运行mongodb,则可以使用:

var url = new MongoUrl("mongodb://localhost:27017");
var client = new MongoClient(url);

You need to get rid of the "mongodb://" part of your host parameter. 您需要摆脱主机参数的“ mongodb://”部分。 So instead of 所以代替

settings.Server = new MongoServerAddress("mongodb://x.x.x.x", 27017);

use 采用

settings.Server = new MongoServerAddress("x.x.x.x", 27017);

or even just 甚至只是

settings.Server = new MongoServerAddress("x.x.x.x");

since 27017 is the default port. 因为27017是默认端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM