我正在我的网站后端工作,我需要从我的 SQL Server 获取特定信息。
为此,我使用 HTTP Get 请求。但我不想使用 ID 来获取信息,但我想键入一个名称来搜索该名称的数据库。
这是我当前的代码:
// GET: api/Users/Toolname
[HttpGet]
public async Task<ActionResult<Users>> Get(string Tool_Name)
{
var info = await _context.Tool_Registration.FindAsync(Tool_Name);
if (info == null)
{
return info;
}
return info;
}
因此,当我输入一个工具名称时,它应该返回使用该工具名称的所有信息。但它不会返回任何东西。当我将它改回 ID 时,我可以搜索 ID 并返回我想要的信息。
如何为我的工具名称发出正确的 HTTP get 请求?
编辑:
关于模型的更多信息:
public class Users
{
[Key]
public int Tool_ID { get; set; }
[Required]
[Column(TypeName = "varchar(MAX)")]
public string Tool_Name { get; set; }
[Column(TypeName = "int")]
public int UUID { get; set; }
[Column(TypeName = "varchar(MAX)")]
public string Name { get; set; }
[Column(TypeName = "varchar(MAX)")]
public string Surname { get; set; }
}
Tool_Registration 是表的名称。该模块存在列名。
编辑2:
这是我的完整控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using iTools_Web_API.Models;
namespace iTools_Web_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly UsersContext _context;
public UsersController(UsersContext context)
{
_context = context;
}
[HttpGet]
[Route("api/Users/{UserId}")]
public async Task<ActionResult<Users>> GetUserById(int UserId)
{
var user = await _context.Tool_Registration.FindAsync(UserId);
return Ok(user);
}
[HttpGet]
[Route("api/Users/")]
public async Task<ActionResult<IEnumerable<Users>>> GetUsers(string Tool_Name)
{
IEnumerable<Users> users;
if (string.IsNullOrEmpty(Tool_Name))
users = await _context.Tool_Registration.ToListAsync();
else
users = await _context.Tool_Registration.Where(tr => tr.Tool_Name == Tool_Name).ToListAsync();
return Ok(users);
}
// PUT: api/Users/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutUsers(int id, Users users)
{
if (id != users.Tool_ID)
{
return BadRequest();
}
_context.Entry(users).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UsersExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Users
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Users>> PostUsers(Users users)
{
var Tool = await _context.Tool_Registration.FindAsync(users.Tool_Name);
if(Tool != null)
{
return Tool;
}
else
{
return NotFound();
}
}
// DELETE: api/Users/5
[HttpDelete("{id}")]
public async Task<ActionResult<Users>> DeleteUsers(int id)
{
var users = await _context.Tool_Registration.FindAsync(id);
if (users == null)
{
return NotFound();
}
_context.Tool_Registration.Remove(users);
await _context.SaveChangesAsync();
return users;
}
private bool UsersExists(int id)
{
return _context.Tool_Registration.Any(e => e.Tool_ID == id);
}
}
}
实体框架的FindAsync
方法根据主键返回一行。当您将输入从 ID 更改为Tool_Name
您不再将方法传递给主键。因此,您需要使用另一种 EF 方法,例如FirstOrDefault
根据 Tool_Name 返回详细信息:
var info = await _context.Tool_Registration.FirstOrDefaultAsync( tr => tr.Tool_Name == Tool_Name);
您可能还应该实现两个端点:一个根据 ID 返回单个用户记录,另一个根据 Tool_Name 过滤器返回多个:
[HttpGet]
[Route("{UserId}")]
public async Task<ActionResult<Users>> GetUserById(int UserId)
{
var user = await _context.Tool_Registration.FindAsync(UserId);
return Ok(user);
}
[HttpGet]
[Route("")]
public async Task<ActionResult<IEnumerable<Users>>> GetUsers(string Tool_Name)
{
IEnumerable<Users> users;
if (string.IsNullOrEmpty(Tool_Name))
users = await _context.Tool_Registration.ToListAsync();
else
users = await _context.Tool_Registration.Where(tr => tr.Tool_Name == Tool_Name).ToListAsync();
return Ok(users);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句