【问题标题】:Serializing a DbGeography type in MVC在 MVC 中序列化 DbGeography 类型
【发布时间】:2015-04-22 16:51:39
【问题描述】:

我在序列化 DbGeography 类型时遇到了问题。我的控制器操作总是给出错误而不是对象。

举个例子,这是我的课:

public class Centre
{
  public int CentreId { get; set; }

  [Required]
  public string Name { get; set; }

  [Required]
  [JsonConverter(typeof(DbGeographyConverter))]
  public DbGeography Location { get; set; }
}

我的 DbGeographyConverter 类是根据周围的许多示例定义的:

public class DbGeographyConverter : JsonConverter
{
  private const string LATITUDE_KEY = "latitude";
  private const string LONGITUDE_KEY = "longitude";

  public override bool CanConvert(Type objectType)
  {
    return objectType.Equals(typeof(DbGeography));
  }

  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  {
    if (reader.TokenType == JsonToken.Null)
      return default(DbGeography);

    var jObject = JObject.Load(reader);

    if (!jObject.HasValues || (jObject.Property(LATITUDE_KEY) == null || jObject.Property(LONGITUDE_KEY) == null))
      return default(DbGeography);

    string wkt = string.Format(CultureInfo.InvariantCulture, "POINT({1} {0})", jObject[LATITUDE_KEY], jObject[LONGITUDE_KEY]);
    return DbGeography.FromText(wkt, DbGeography.DefaultCoordinateSystemId);
  }

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    var dbGeography = value as DbGeography;

    serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : new { latitude = dbGeography.Latitude.Value, longitude = dbGeography.Longitude.Value });
  }
}

然后,在我的控制器类 (CentreController) 中,我有以下几行:

public async Task<JsonResult> Get(int Id)
{
  var centre = new Centre {CentreId = Id};
  // This is a call to our data service, which basically loads the centre object
  // from the database via Entity Framework.
  centre = (await CentreService.Get(centre)).Data;

  //centre.Location = null;
  //var jc = JsonConvert.SerializeObject(centre);

  return Json(centre,JsonRequestBehavior.AllowGet);
}

这给了我一个 500 错误和一个关于 Null 值的异常。

如果我取消注释该行

//centre.Location = null;

然后代码运行正常,但没有位置被传回给调用者。

如果我取消注释该行

//var jc = JsonConvert.SerializeObject(centre);

然后我的 DbGeographyConverter 被调用,并且 jc 对象包含我想要返回的正确数据,但在字符串而不是 JsonResult 中。对 Json 的调用没有调用 DbGeographyConverter,我不知道为什么。

如何获得 Json 调用来运行我的自定义转换器?或者,如何让我的控制器方法将 JsonConvert 的输出转换为 JsonResult?

【问题讨论】:

  • 使用控制器的Json 方法时没有调用转换器的原因是因为MVC 默认不使用Json.Net。您可以执行@py3r3str 建议的操作,也可以实现自己的 JsonNetResult 来处理此问题。见this question

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


【解决方案1】:

您可以使用 Json.NET 转换为 json,然后使用 Content 方法返回转换后的字符串。记得添加适当的内容类型。

public async Task<ActionResult> Get(int Id)
{
    var centre = new Centre {CentreId = Id};
    centre = (await CentreService.Get(centre)).Data;

    var jc = JsonConvert.SerializeObject(centre);

    Response.ContentType = "application/json";
    return Content(jc)
}

它适合你吗?

【讨论】:

    【解决方案2】:

    使用 Json.NET nuget 包,默认的 System.Web.Mvc.Json 方法无法处理来自 DbGeography 的空值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2016-02-20
      • 2012-03-11
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多