[OperationContract]
public FareDataModel GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return FareManager.GetFare(source, destination, country, city);
}
我在我的 svc 类中有上述方法。但这无法在 Json 中完全发送数据。我得到以下结果
{
"d": {
"__type": "FareDataModel:#MeterupAutoTaxiWebService",
"Fares": [
{
"__type": "VehicleFare:#MeterupAutoTaxiWebService"
}
]
}
}
但是当我将它作为字符串返回时,我得到的是字符串形式的数据。我真的很想得到 json。这是我的 FareDataModel 对象
[DataContract]
public class FareDataModel
{
public string SourceAddress;
public string DestinationAddress;
public int Duration;
public int Distance;
public string Error;
[DataMember]
public List<VehicleFare> Fares = new List<VehicleFare>();
}
[DataContract]
public class VehicleFare
{
public string PhoneNo;
public string Vehicle;
public double Fare;
}
我尝试了其中一篇文章中解释的所有更改,但对我没有任何帮助。当我通过如下修改我的代码以字符串形式返回时
[OperationContract]
public string GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));
}
我得到以下结果
{
"d": "{\"SourceAddress\":\"Majestic, Bangalore, Karnataka, India\",\"DestinationAddress\":\"Marathahalli Market, Bangalore, Karnataka, India\",\"Duration\":2475,\"Distance\":19872,\"Error\":null,\"Fares\":[{\"PhoneNo\":null,\"Vehicle\":\"Auto\",\"Fare\":238}]}"
}
我真的需要一个真正的 json 响应。
这是我的课
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public string GetFareAndDistanceJson(string source, string destination, string country, string city)
{
return new JavaScriptSerializer().Serialize(FareManager.GetFare(source, destination, country, city));
}
// Add more operations here and mark them with [OperationContract]
}
请帮帮我。
我的网络配置
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MeterupAutoTaxiWebService.Service1">
<endpoint address="" behaviorConfiguration="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="MeterupAutoTaxiWebService.Service1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MeterupAutoTaxiWebService.Service1AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<connectionStrings>
<add name="MeterupAutoTaxiEntities" connectionString="metadata=res://*/MeterupTaxiAutoEntities.csdl|res://*/MeterupTaxiAutoEntities.ssdl|res://*/MeterupTaxiAutoEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=MeterupAutoTaxi;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>