新的类对象列表给出 System.NullReferenceException

问题描述 投票:0回答:1

我正在将我的项目从 ASP.NET 4.8 升级到 ASP.NET Core 6.0。下面的代码适用于 ASP.NET,但对于 Core 会出错。我不确定为什么。我尝试了很多不同的方法来解决它,但到目前为止没有运气。

当我尝试导航到它的资源时出现错误:

http://localhost/odata/Users
.

UserService.cs

namespace pal_data.Business
{
    public class UserService
    {
        public List<User> GetData()
        {
            try
            {
                var lsret = new List<User>();   // ERROR: System.NullReferenceException: Object reference not set to an instance of an object.

                using (SqlConnection con = new(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnPort"].ConnectionString))
                {
                    var strSQL = "select a.*, b.RoleName from AppUser a inner join AccessRole b on a.RoleID = b.AccessRoleID";

                    using (SqlDataAdapter da = new(strSQL, con))
                    {
                        var dt = new DataTable();
                        da.Fill(dt);

                        foreach (DataRow dr in dt.Rows)
                        {
                            var item = new User
                            {
                                UserID = int.Parse(dr["AppUserID"].ToString() ?? ""),
                                RoleID = int.Parse(dr["RoleID"].ToString() ?? ""),
                                Role = dr["RoleName"].ToString() ?? "",
                                UserName = dr["UserName"].ToString() ?? "",
                                Name = dr["Name"].ToString() ?? "",
                                Password = dr["Password"].ToString()
                            };

                            lsret.Add(item);
                        }
                    }
                }

                return lsret;
            }
            catch (SqlException ex)
            {
                // ...
            }
            catch (Exception e)
            {
                // ...
            }
        }
    }
}

UsersController.cs

namespace pal_data.Controllers
{
    [EnableCors]
    public class UsersController : ODataController
    {
        private readonly UserService m_service = new();

        [EnableQuery]
        public ActionResult<IEnumerable<User>> Get()
        {
            return Ok(m_service.GetData());
        }
    }
}

User.cs

namespace pal_data.Models
{
    public class User
    {
        public int UserID { get; set; }
        public string? FunctionResult { get; set; }
        public int ReturnCode { get; set; }
        public int RoleID { get; set; }
        public string? Role { get; set; }
        public string? UserName { get; set; }
        public string? Name { get; set; }
        public string? Password { get; set; }
    }
}

Working UserService.cs

  • 这会返回没有空错误的数据
namespace pal_data.Business
{
    public class UserService
    {
        public List<User> GetData()
        {
            var lsret = new List<User>
            {
                new User
                {
                    UserID = 1,
                    RoleID = 1,
                    Role = "asdfsdf",
                    UserName = "sadfsdf",
                    Name = "sdfasdf",
                    Password = "asdfsdf",
                }
            };

            return lsret;
        }
    }
}
c# asp.net-core odata nullreferenceexception
1个回答
0
投票

因为代码在 SQL 查询上失败,错误显示在这一行

var lsret = new List<User>();
出于某种原因。我猜是吸取了教训。我的 SQL 查询现在已正确编写(不使用
ConfigurationManager
)并且实体现在可以正确返回。

© www.soinside.com 2019 - 2024. All rights reserved.