【问题标题】:GET method returning emptyGET 方法返回空
【发布时间】:2021-01-17 04:20:07
【问题描述】:

晚安,我正在开发 WebApi,我使用的是代码优先的方法,为此我使用了实体框架 6.0.1,在我的项目中,我有以下类 PersonController.cs、Configurations.cs(我现在手动插入数据的地方)和我的 Person 类,我正在使用 Postman 来模拟请求,我在网站上查找了一些示例,但没有与我的示例相同,如果有人可以帮助我,谢谢。

PersonController.cs

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
using WebApi.Contexto;

namespace WebApi.Controllers
{
    public class PersonController : ApiController
    {
        private readonly Context contexto = new Context();
        [Route("api/person")]
        [HttpGet]
        public IHttpActionResult getAll(string search)
        {
            if (string.IsNullOrEmpty(search))
            {
                search = "";
            }
            var list = contexto.People.Where(x => x.firstName.Contains(search) || x.lastName.Contains(search));

            return Ok(list);

        } 
     }
}

Configurations.cs

namespace WebApi.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebApi.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<WebApi.Contexto.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(WebApi.Contexto.Context context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.

            context.People.Add(new Person { firstName = "Andrew ", lastName = "Teste", id = 1, birthDate = new DateTime(2021, 01, 31) });
           
        }
    }
}

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApi.Models
{
    public class Person
    {
        public int id { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public DateTime birthDate { get; set; }
    }
}

【问题讨论】:

  • 您访问的具体网址是什么?

标签: c# .net entity-framework-6


【解决方案1】:

调用api时搜索参数为null

为此进行的一项工作是创建一个简单的模型或类 喜欢

public class Query
{
   public string Search {get;set;}
}

把你的方法改成

public IHttpActionResult getAll([FromBody]Query query)
{
        var search = query.Search;

        if (string.IsNullOrEmpty(search))
        {
            search = "";
        }

        var list = contexto.People.Where(x => x.firstName.Contains(search) || 
        x.lastName.Contains(search));

        return Ok(list);

} 

你的 Postman 有效载荷应该是这样的

{
   "search":"a"
}

【讨论】:

    【解决方案2】:

    只是告诉你,我的错误是在数据库连接字符串中发现的,它没有参数:

    Integrated Security = True;

    感谢大家的帮助

    【讨论】:

      【解决方案3】:

      您缺少 [HttpGet] 属性中的参数,因此您的方法参数实际上并未获取您的查询字符串。

      试试

      [HttpGet("{search}")]
      

      public IHttpActionResult GetAll([FromQuery] string search)
      

      【讨论】:

      • 我尝试这样做,但它返回此错误 CS1729: 'HttpGetAttribute' does not contain a constructor that take 1 arguments
      • @JheimisMarques 您使用的是 .NET Core 还是 Framework?
      • @SoyDuck 可能认为你在 Core 上。
      • 如果他在 aspnet 框架上,那么可以试试public IHttpActionResult GetAll([FromQuery] string search)
      • tbf 没有人应该在 2021 年为任何新项目使用框架。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多