我正在使用ASP.Net API和数据库优先方法为有角度的前端应用程序构建Web api。当我在邮递员中测试GetPeoplez https://localhost:44333/api/people/GetPeoplez
(应该是人的名单)URL时,它可以工作,但对于控制器的其他方法,它始终显示HTTP错误400
这是我实现控制器的方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TestApi.Collection;
using TestApi.Models;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace TestApi.Controllers
{
[Route("api/[controller]")]
[Controller]
public class PeopleController : Controller
{
IPeopleCollection peopleCollection;
public PeopleController(IPeopleCollection _peopleCollection)
{
peopleCollection = _peopleCollection;
}
[HttpGet]
[Route("GetPeoplez")]
public async Task<IActionResult> GetPeoplez()
{
try
{
var peoplez = await peopleCollection.GetPeoplez();
if (peoplez == null)
{
return NotFound();
}
return Ok(peoplez);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpGet]
[Route("GetPeople")]
public async Task<IActionResult> GetPeople(int? peopleId)
{
if (peopleId == null)
{
return BadRequest();
}
try
{
var people = await peopleCollection.GetPeople(peopleId);
if (people == null)
{
return NotFound();
}
return Ok(people);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpPost]
[Route("AddPeople")]
public async Task<IActionResult> AddPoeple([FromBody]People model)
{
if (ModelState.IsValid)
{
try
{
var peopleId = await peopleCollection.AddPeople(model);
if (peopleId > 0)
{
return Ok(peopleId);
}
else
{
return NotFound();
}
}
catch (Exception)
{
return BadRequest();
}
}
return BadRequest();
}
[HttpDelete]
[Route("DeletePeople")]
public async Task<IActionResult> DeletePeople(int? peopleId)
{
int result = 0;
if (peopleId == null)
{
return BadRequest();
}
try
{
result = await peopleCollection.DeletePeople(peopleId);
if (result == 0)
{
return NotFound();
}
return Ok();
}
catch (Exception)
{
return BadRequest();
}
}
[HttpPut]
[Route("UpdatePeople")]
public async Task<IActionResult> UpdatePeople([FromBody]People model)
{
if (ModelState.IsValid)
{
try
{
await peopleCollection.UpdatePeople(model);
return Ok();
}
catch (Exception ex)
{
if (ex.GetType().FullName ==
"Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException")
{
return NotFound();
}
return BadRequest();
}
}
return BadRequest();
}
}
}
我用来访问api的网址是:
https://localhost:44333/api/people/GetPeople?id=x
for a single people https://localhost:44333/api/people/UpdatePeople?id=x
以更新元素 https://localhost:44333/api/people/DeletePeople?id=x
删除元素
我对CRUD方法的实现是非常基本的
使用Microsoft.AspNetCore.Mvc; 使用Microsoft.EntityFrameworkCore; 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Threading.Tasks; 使用TestApi.Models; 使用TestApi.ViewModel;
命名空间TestApi.Collection { 公共类PeopleCollection:IPeopleCollection { PeopleContext db;
public PeopleCollection(PeopleContext _db)
{
db = _db;
}
public async Task<int> AddPeople(People people)
{
if (db != null)
{
await db.People.AddAsync(people);
await db.SaveChangesAsync();
return people.Id;
}
return 0;
}
public async Task<int> DeletePeople(int? peopleId)
{
int result = 0;
if (db != null)
{
//Find the post for specific post id
var people = await db.People.FirstOrDefaultAsync(x => x.Id == peopleId);
if (people != null)
{
//Delete that post
db.People.Remove(people);
//Commit the transaction
result = await db.SaveChangesAsync();
}
return result;
}
return result;
}
public async Task<List<People>> GetPeoplez()
{
if (db != null)
{
return await db.People.ToListAsync();
}
return null;
}
public async Task<People> GetPeople(int? peopleId)
{
return await db.People.Where(p => p.Id == peopleId).FirstOrDefaultAsync();
}
public async Task UpdatePeople(People people)
{
if (db != null)
{
//Delete that post
db.People.Update(people);
//Commit the transaction
await db.SaveChangesAsync();
}
}
}
}
关于如何克服此错误的任何想法?谢谢
答案 0 :(得分:1)
给出400 msg意味着错误的请求错误,因为它遇到了一些异常/错误,因此进入了您指定的返回错误请求的catch块....尝试调试器并调试您的代码
答案 1 :(得分:0)
您要发送的不是整数的X
Emir Hadžihafizbegović
除非这只是一个示例,否则您实际上是在发送号码。...