HTTP 获取请求 SQL 服务器 Web API

穆伦贝尔德

我正在我的网站后端工作,我需要从我的 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);
    }
}
}
严格的01

实体框架的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将iPhone HTTP请求发送到Apache PHP Web服务器

Apache HTTP Server背后的Glassfish服务器Web服务

具有HttpCore的Web服务器的HTTP请求/响应流

无法获取Web服务器的PID

对ASP.NET Web API的请求的“透明”服务器端代理

发送HTTP标头后服务器无法设置状态-Web API CORS

无法使用twisted.web.AGENT向Django服务器发出HTTP请求

从API服务器请求资源

我应该使用服务器密钥还是旧版服务器密钥通过HTTP1.1将发布请求发送到FCM Api?

Web服务器如何知道何时完全接收到HTTP请求?

如何为多个SQL Server数据获取请求设置API服务器?

如何解决向特定Web服务器发出HTTP请求的抖动Web HTTP错误

从API服务器获取400请求

在服务器上的dotnet core 3.1 Web API开发sql连接失败

是否可以在PHP的内置Web服务器中启用HTTP范围请求支持?

AWS无服务器:基于请求或HTTP API参数强制并行执行lambda

(500)内部服务器错误-使用api发送Web请求时

从Web客户端到API服务器的跨源请求

对我的HTTP服务器的奇怪请求

HTTP从API获取对XML的请求

对asp.net Web API的POST请求达到某些限制时,服务器无响应

Angular2 $ http获取具有路由属性的Web api控制器的请求

Soap UI 5.3.0:RESTful API - 如何找到通过 Restful API 公开的 Web 服务器的 Web 请求的输入参数

Web api 内部服务器错误

HTTP 请求未命中 WEB API 2 控制器

在flutter Web应用程序中对http和https API服务器的http GET请求发生异常

(Heroku) 如何从我的 Web dyno 向我的服务器 dyno 发送 HTTP 请求

反向代理服务器可以接受 https 请求并通过 http 转发到 Web 服务器吗?

React Native:http 服务器上的请求 api 不起作用