我是.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; }
}
}
BookController.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);
}
}
}
答案 0 :(得分:1)
仅适用于ASP.Net核心部分:
localhost:63654 / api / book {“Title”:“Deep Learning”,“CategoryCodes”:“5”}
book
为空
如何提出您的请求(在您的情况下,Postman
)很重要 - 这就是我要求您提供的原因。所以Headers
等等很重要。
通过邮递员发送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
标题
CategoryCodes
模型中的 Book
为List
,因此您必须为其值设置“列表”(数组),如上所示"CategoryCodes": ["1","2","3"]
H个..