我有一个我序列化的DTO类
Json.Serialize(MyClass)
Run Code Online (Sandbox Code Playgroud)
我怎样才能排除它的公共财产?
(它必须是公开的,因为我在其他地方的代码中使用它)
我的模型中有一个圆形对象图,但这是不可避免的.
根据本文中给出的建议,我使用了所有成员DataContractAttribute并设置IsReference = true了所有成员.我还提供了DataMemberAttribute我要序列化的所有属性.
为了确保序列化程序不再出现任何问题,我只选择不序列化导航属性.
但是,我的catch块中仍然遇到异常.例外的细节如下:
_innerException: {"Type
'System.Data.Entity.DynamicProxies.Author_615FB9F8BB22B55A7CA168DA5ED29EC6A0B59F62FD79D1346045351BE2F163A4' with data contract name
'Author_615FB9F8BB22B55A7CA168DA5ED29EC6A0B59F62FD79D1346045351BE2F163A4:
http://schemas.datacontract
.org/2004/07/System.Data.Entity.DynamicProxies' is not expected.
Consider using a
DataContractResolver or add any types not known statically to
the list of known types - for
example, by using the KnownTypeAttribute attribute or by adding them
to the list of known types
passed to DataContractSerializer."}
Run Code Online (Sandbox Code Playgroud)
我可以但不希望:
1)禁用代理创建.我可以删除代理创建只是为了序列化,我可能会这样做.但我也想知道为什么我仍然得到例外以及我能做些什么.
2)删除循环引用.原因:这些类型的引用在Entity Framework生成的模型中非常常见.如果我要在模型中做一个800 - 1000个类的大项目,那么通过删除循环引用来实现它是一个噩梦.
我在下面描述了这个小尖峰解决方案的架构元素.
数据库架构
Id AuthorName
-------------------------------
1 Charles …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc serialization asp.net-mvc-4 asp.net-web-api
我有一个Asp.Net WebApi项目,我想返回Json格式的产品列表和一个特定的产品。
这是我的产品型号:
public class Product
{
public int Id { get; set; }
public string ShortString { get; set; }
public string LongString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的ApiController:
public class ProductController : ApiController
{
public IQueryable<Product> Get()
{
return Context.Products;
}
public IHttpActionResult Get(int id)
{
var p = Context.Products.FirstOrDefault(m => m.Id == id);
if (p == null)
return NotFound();
return Ok(p);
}
}
Run Code Online (Sandbox Code Playgroud)
我想返回LongString一种特定产品中的字段,而不是产品列表中的字段。[JsonIgnore]Json.Net库中是否有任何条件属性。
我有一个带有.Net Core的新项目.这是一个WebAPI项目.我的模型有一个单独的项目.
在WebAPI项目中,在控制器中,我有这样的东西:
// GET: api/questions
[HttpGet]
public IEnumerable<Question> GetQuestions()
{
return _context.Questions
.Include( i => i.QuestionType );
}
Run Code Online (Sandbox Code Playgroud)
当我调用http://localhost:55555/api/questios/它时只返回第一条记录,然后出现此错误消息:Recv failure:Connection was reset
如果我删除Include部件并返回_context.Questions,它工作得很好!
我的代码有什么问题?