簡體   English   中英

如何使用 .net core 和 neo4j 進行 Post 操作

[英]How to make Post operation using .net core and neo4j

我是 .Net Core 和 Neo4j 的新手。 我想使用 Neo4jClient 為 CRUD 操作制作 Wep Api。我有一個 Book 類及其控制器。 我還有 Author 及其控制器類。 我想進行Post操作,但我不能。 郵遞員返回 BadRequest()。 那么我該如何解決這個問題? 問題是什么?

這是我的 Book.cs 源代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NEO4j.Models
{
    public class Book
    {

        public int Id { get; set; }
        public string Title { get; set; }
        public List<string> CategoryCodes { get; set; }
    }
}

圖書控制器.cs

using Microsoft.AspNetCore.Mvc;
using NEO4j.Models;
using NEO4j.Services;
using Neo4jClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace NEO4j.Conrollers
{
    [Route("api/book")]
    public class BookController : Controller
    {
        private static int uniqueId=1;
        public string title { get; set; }
        public List<string> category { get; set; }

        private static BookService bookService;
        private static CategoryService categoryService;

        static BookController()
        {
            bookService = new BookService();
            categoryService = new CategoryService();
        }

        [HttpGet]
        public ActionResult Get()
        {
            var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "1234");
            graphClient.Connect();

            var data = graphClient.Cypher
               .Match("(m:Book)")
               .Return<Book>("m")
               .Results.ToList();

            return Ok(data.Select(c => new { book = c }));
        }


        [HttpPost]
        public ActionResult Post([FromBody] Book book) {

            var client = new GraphClient(new Uri("http://localhost:7474/db/data"), "neo4j", "1234");
            client.Connect();


            if (book == null)
                return BadRequest();

            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var newBook = new Book { Id = uniqueId, Title = title, CategoryCodes = category };
            client.Cypher
                .Merge("(book:Book { Id: {uniqueId} ,Title:{title},CategoryCodes:{category}})")
                .OnCreate()
                .Set("book = {newBook}")
                .WithParams(new
                {
                    uniqueId =newBook.Id,
                    title = newBook.Title,
                    category = newBook.CategoryCodes,
                    newBook
                })
                .ExecuteWithoutResults();

             uniqueId++;
             return Ok(newBook);
        }

    }
}

僅適用於 ASP.Net Core 部分:

本地主機:63654/api/book {“標題”:“深度學習”,“類別代碼”:“5”}

book為空

  1. 如何提出請求(就您而言, Postman )很重要 - 這就是我要求您提供的原因。 所以Headers等很重要。

    通過 Postman 發送JSON

     POST /api/test HTTP/1.1 Host: localhost:63654 Content-Type: application/json Cache-Control: no-cache Postman-Token: 51081616-c62d-c677-b5ab-37d428018db5 {"Title":"Deep Learning", "CategoryCodes": ["1","2","3"]}

    注意Content-Type標題

  2. 我們的Book模型中的CategoryCodes是一個List ,因此您必須為其值設置一個“列表”(數組),如上所示"CategoryCodes": ["1","2","3"]

嗯..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM