Veljanovski
Veljanovski

Reputation: 43

Trying to connect Neo4j with ASP.NET Core

I'm having trouble connecting Neo4j with my ASP.NET Core Web App. I've installed Neo4j.Driver package, I've added

"NeO4jConnectionSettings": {
  "Server": "bolt://localhost:7687",
  "UserName":"neo4j",
  "Password":"<password>"     
  }

into appsettings.json. I also have

services.AddSingleton(GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "<password>")));

in ConfigureServices method in Startup.cs. In Index.cshtml.cs I am runnning

public class IndexModel : PageModel
{
    private readonly IDriver _driver;

    private readonly ILogger<IndexModel> _logger;
    public string result;

    public IndexModel(ILogger<IndexModel> logger, IDriver driver)
    {
        _logger = logger;
        _driver = driver;
    }

    public async void GetAsync()
    {
        IResultCursor cursor;
        var list = new List<String>();
        IAsyncSession session = _driver.AsyncSession();
        try
        {
            cursor = await session.RunAsync(@"MATCH (a:Movie)
            RETURN a.name as name
            limit 10");
            list = await cursor.ToListAsync(record =>
            record["name"].As<String>());
        }
        finally
        {
            await session.CloseAsync();
        }
        result = list.FirstOrDefault();
    }
}

which returns no results. What am I missing?

Upvotes: 0

Views: 863

Answers (1)

zhulien
zhulien

Reputation: 5695

You have trouble connecting or you get no result?

It seems like you're just getting an empty result. Looking at your query, I am pretty sure you're using the default Actors/Movies database set. If so, the Movie entity doesn't contain a name property. You need to use title.

try
{
    cursor = await session.RunAsync(@"MATCH (m:Movie)
        RETURN m.title AS title
        LIMIT 10");

    list = await cursor.ToListAsync(record =>
    record["title"].As<String>());
}

Also, as you're using Razor Pages, if you're using the default convention for method name handling, your GetAsync method should be called OnGetAsync/OnGet.

Upvotes: 2

Related Questions