【发布时间】:2016-03-21 03:35:00
【问题描述】:
我正在尝试将 [Serializable] 属性与 PetaPoco 实体一起使用,以便可以将其存储在 REDIS 缓存中。
将 [Serializable] 属性添加到 PetaPoca 实体类允许它使用 REDIS 缓存提供程序进行缓存,但随后会更改 WebAPI 将实体序列化为 JSON 的方式,这会中断。
有没有办法指定 WebAPI 如何将对象序列化为 JSON?
以下示例,具体而言,这是 DNN 的 DAL2 中并使用 DNN 的 https://github.com/davidjrh/dnn.rediscachingprovider REDIS 缓存提供程序。
DNN DAL2 / PetaPoco 对象:
[TableName("sbCurrencyRates")]
//setup the primary key for table
[PrimaryKey("CurrID", AutoIncrement = true)]
//configure caching using PetaPoco
[Cacheable("sbCurrencyRates", CacheItemPriority.Default, 20)]
//scope the objects to the ModuleId of a module on a page (or copy of a module on a page)
[Serializable]
public class CurrRateInfo
{
public int CurrID { get; set; }
public string CurrCode { get; set; }
public double CurrRate { get; set; }
public DateTime LastUpdate { get; set; }
}
WebApi 控制器:
public class RatesController : DnnApiController
{
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetRates()
{
CurrRatesController crc = new CurrRatesController();
return Request.CreateResponse(HttpStatusCode.OK, crc.Get());
}
}
提前致谢。
【问题讨论】:
-
HttpGet 和 HttpPost 属性不能一起使用。
标签: c# json serialization asp.net-web-api dotnetnuke