【问题标题】:send JSON Object in ASP .NET在 ASP .NET 中发送 JSON 对象
【发布时间】:2020-07-24 12:43:06
【问题描述】:

我正在尝试以 JSON 格式发送一个对象,但该方法总是得到一个空对象

这是错误:

Server Error in '/' Application.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Source Error: 


Line 15:             context = ApplicationDbContext.Create();
Line 16:             context.Users.Add(user);
Line 17:             if (context.SaveChanges() == 0)
Line 18:             {
Line 19:                 context.Dispose();

Source File: D:\Education\Git\Online-Store-Platform-API\OnlineStorePlatform\OnlineStorePlatform\DBContext\UserContext.cs    Line: 17 

这是我的代码:

public class AccountController : Controller
    {
        //public UserDTO user;
        public UserContext myContext;
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }
        //[HttpPost]
        public ActionResult Register([FromBody]UserDTO user)
        {
            //user = new UserDTO(email, password, userName);
            myContext = new UserContext();
            if (myContext.addUser(new User(user)) == null) return Content("ERROR");
            return Content("Created Successfully");
        }
    }

当我发送一个 UserDTO 对象时,总是得到 null

我的要求:

使用DTO类:

public class UserDTO
    {
        public String email, userName, password;
        public UserDTO(String email, String password, String userName)
        {
            this.email = email;
            this.password = password;
            this.userName = userName;
        }
        public UserDTO() { }
    }

当我尝试调试时,我得到了一个空对象

【问题讨论】:

  • 对于email, userName, password 使用带有getter 和setter 的属性,例如public string email{get;set;}

标签: c# asp.net json asp.net-mvc


【解决方案1】:

好的,我找到了解决问题的方法.. 问题是我继承自类 Controller 但我应该继承自 ApiController 。

public class AccountController : Controller
    {
        ........
    }

在那之后它工作了..谢谢大家

【讨论】:

    【解决方案2】:
    public class AccountController : Controller
    {
        public UserContext myContext;
    
        public AccountController () : this(new UserContext()) {}
    
        public AccountController (UserContext _myContext) 
        {
           myContext = _myContext;
        }
    
    
        // GET: Account
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Register(UserDTO user)
        {
            if (ModelState.IsValid) {
               myContext.Add(User);
              //ToDo: Redirect or many options
            }
            return View(user);
        }
    }
    

    编辑:

    这很重要

    public class UserDTO
    {
        public String email { get; set; }
        public String userName  { get; set; }
        public String password  { get; set; }
     }
    

    【讨论】:

    • 试过了还是不行,请看编辑
    • 属性是必需的。
    【解决方案3】:

    将电子邮件、用户名和密码转换为属性。

    公共字符串电子邮件{获取;设置;}

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多