我有 2 个简单的 web 服务方法来查询我通过 javascript 查询的 XML 文档(存储在 HTTP 缓存中)。GetCitiesForAffiliate()
当我为 aff 参数传入一个完全有效的数据项时,该方法会在“选择新”行上抛出 NullReferenceException。它适用于 aff 参数中的其他数据。另一种方法也可以正常工作,即使使用相同的 aff 参数会导致另一种方法炸毁。
我刚刚验证了我正在查询的 XML。当我传入一个不存在的 aff 时,这两种方法都只返回一个空的 json 字符串,这没关系。我应该看什么可能是错的?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCitiesForAffiliate(string aff)
{
LocationService loc = new LocationService();
var query = (from center in loc.centersXml.Descendants("Center")
where center.Element("ServiceArea").Value.Equals(aff)
select new {
City = center.Element("City").Value
}).Distinct().OrderBy(x => x.City);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(query);
return json;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCentersForAffiliateCity(string aff, string city)
{
LocationService loc = new LocationService();
var query = (from center in loc.centersXml.Descendants("Center")
where center.Element("ServiceArea").Value.Equals(aff) && center.Element("City").Value.Equals(city)
select new {
ID = center.Element("ID").Value,
Name = center.Element("Name").Value
}).Distinct().OrderBy(x => x.Name);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(query);
return json;
}