首页 > 解决方案 > System.Runtime.Serialization.SerializationException

问题描述

当我将控制器添加到(获取、放置、发布、删除)时,我得到了同样的错误 在此处输入图像描述

控制器代码是

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using YourRealityAPI.Models;

namespace YourRealityAPI.Controllers
{
    public class T_UsersController : ApiController
    {
        private YourRealityDBEntities db = new YourRealityDBEntities();

        // GET: api/T_Users
        public IQueryable<T_Users> GetT_Users()
        {
            return db.T_Users;
        }

        // GET: api/T_Users/5
        [ResponseType(typeof(T_Users))]
        public IHttpActionResult GetT_Users(int id)
        {
            T_Users t_Users = db.T_Users.Find(id);
            if (t_Users == null)
            {
                return NotFound();
            }

            return Ok(t_Users);
        }

        // PUT: api/T_Users/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutT_Users(int id, T_Users t_Users)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != t_Users.U_id)
            {
                return BadRequest();
            }

            db.Entry(t_Users).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!T_UsersExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/T_Users
        [ResponseType(typeof(T_Users))]
        public IHttpActionResult PostT_Users(T_Users t_Users)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.T_Users.Add(t_Users);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = t_Users.U_id }, t_Users);
        }

        // DELETE: api/T_Users/5
        [ResponseType(typeof(T_Users))]
        public IHttpActionResult DeleteT_Users(int id)
        {
            T_Users t_Users = db.T_Users.Find(id);
            if (t_Users == null)
            {
                return NotFound();
            }

            db.T_Users.Remove(t_Users);
            db.SaveChanges();

            return Ok(t_Users);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool T_UsersExists(int id)
        {
            return db.T_Users.Count(e => e.U_id == id) > 0;
        }
    }
}

并且数据库名称“T_Users”类(T_Users.cs)中的表是

    //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace YourRealityAPI.Models
{
    using System;
    using System.Collections.Generic;

    public partial class T_Users
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public T_Users()
        {
            this.T_Clients = new HashSet<T_Clients>();
            this.T_Projects = new HashSet<T_Projects>();
        }

        public int U_id { get; set; }
        public string U_username { get; set; }
        public string U_password { get; set; }
        public Nullable<System.DateTime> U_CreationDate { get; set; }
        public Nullable<int> U_type { get; set; }
        public Nullable<bool> U_isBanned { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Clients> T_Clients { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<T_Projects> T_Projects { get; set; }
        public virtual T_UserType T_UserType { get; set; }
    }
}

当我尝试运行项目 API 并遇到此问题时,请 在此处输入图像描述

我怎么能(get,put,post,delete)并且我在每一个中都有相同的错误是什么问题,我该怎么做

标签: apiasp.net-web-api

解决方案


推荐阅读