Asp.net Objective-C将JSON发布到WebApi控制器
我有以下javascript函数,用于将数据发回ASP.NET Web Api控制器:Asp.net Objective-C将JSON发布到WebApi控制器,asp.net,objective-c,json,asp.net-web-api,nsurlconnection,Asp.net,Objective C,Json,Asp.net Web Api,Nsurlconnection,我有以下javascript函数,用于将数据发回ASP.NET Web Api控制器: var d = { NorthEastPoint: { Latitude: boundingBox.ne.lat, Londitude: boundingBox.ne.lng }, SouthWestPoint: { Latitude: boundingBox.sw.lat, Londitude: boundingBox.
var d = {
NorthEastPoint: {
Latitude: boundingBox.ne.lat,
Londitude: boundingBox.ne.lng
},
SouthWestPoint: {
Latitude: boundingBox.sw.lat,
Londitude: boundingBox.sw.lng
},
CurrentLocation: {
Latitude: 0,
Londitude: 0
},
IncludeFullImage: false
};
console.log(JSON.stringify(d));
var endPoint = 'http://127.0.0.1/api/Search/';
$.ajax({
url: endPoint,
type: 'POST',
data: d,
dataType: 'json',
crossDomain: true,
success: function (data) {
},
statusCode: {
404: function (content) {
return 'cannot find resource';
},
505: function (content) {
return 'internal server error';
}
},
error: function (req, status, errorObj) {
// handle status === "timeout"
// handle other errors
}
});
这个帖子很好用。我试图使用NSURLConnection在Objective-C中复制ajax文章
下面是我创建json的Objective-C代码:
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
NSDictionary *NorthEastPoint = [[NSDictionary alloc] initWithObjectsAndKeys:[[NSNumber alloc] initWithFloat:_pointOneLat],@"Latitude",
[[NSNumber alloc] initWithFloat:_pointOneLon],@"Londitude",nil];
NSDictionary *SouthWestPoint = [[NSDictionary alloc] initWithObjectsAndKeys:[[NSNumber alloc] initWithFloat:_pointTwoLat],@"Latitude",
[[NSNumber alloc] initWithFloat:_pointTwoLon],@"Londitude",nil];
NSDictionary *CurrentLocation = [[NSDictionary alloc] initWithObjectsAndKeys:[[NSNumber alloc] initWithFloat:_currentLat],@"Latitude",
[[NSNumber alloc] initWithFloat:_currentLon],@"Londitude",nil];
[dict setObject:NorthEastPoint forKey:@"NorthEastPoint"];
[dict setObject:SouthWestPoint forKey:@"SouthWestPoint"];
[dict setObject:CurrentLocation forKey:@"CurrentLocation"];
[dict setObject:@"false" forKey:@"IncludeFullImage"];
NSError *writeError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsonString);
上面的输出是:
JSON Output: {
"NorthEastPoint" : {
"Londitude" : 1.388894,
"Latitude" : 57.49198
},
"CurrentLocation" : {
"Londitude" : -6.12792,
"Latitude" : 53.24554
},
"SouthWestPoint" : {
"Londitude" : -5.642355,
"Latitude" : 52.30821
},
"IncludeFullImage" : "false"
}
然后,我尝试使用以下代码将json发布到我的WebApi控制器:
NSURL * url = [NSURL URLWithString:@"http://172.20.10.5/apisite/api/Search"];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonData];
theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
[self.delegate searchDidStart];
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
self.receivedData = [[NSMutableData alloc]init];
} else {
}
在控制器上发布模型后,我可以看到我的发布成功,因为达到了断点,但是没有填充模型数据。
我的控制器方法如下所示:
public async Task<IEnumerable<PlaceDTO>> Post(BoxSearchParam model) {
// Do stuff
}
公共异步任务Post(BoxSearchParam模型){
//做事
}
BoxSearchParam的定义如下:
public class BoxSearchParam
{
/// <summary>
/// Initializes a new instance of the <see cref="BoxSearchParam"/> class.
/// </summary>
public BoxSearchParam()
{
this.NorthEastPoint = new MapPoint();
SouthWestPoint = new MapPoint();
CurrentLocation = new MapPoint();
MaximumSearchRadius = int.Parse(ConfigurationManager.AppSettings["MaxDistanceBetweenPoints"]);
}
/// <summary>
/// Gets or sets the north west point.
/// </summary>
public MapPoint NorthEastPoint { get; set; }
/// <summary>
/// Gets or sets the south east point.
/// </summary>
public MapPoint SouthWestPoint { get; set; }
/// <summary>
/// Gets or sets the current location.
/// </summary>
public MapPoint CurrentLocation { get; set; }
/// <summary>
/// Gets or sets the maximum search radius.
/// </summary>
public int MaximumSearchRadius { get; internal set; }
/// <summary>
/// Gets or sets a value indicating whether include full image.
/// </summary>
public bool IncludeFullImage { get; set; }
}
public class MapPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="MapPoint"/> class.
/// </summary>
public MapPoint()
{
Londitude = 0;
Latitude = 0;
}
/// <summary>
/// Gets or sets the londitude.
/// </summary>
public decimal Londitude { get; set; }
/// <summary>
/// Gets or sets the latitude.
/// </summary>
public decimal Latitude { get; set; }
}
公共类BoxSearchParam
{
///
///初始化类的新实例。
///
public BoxSearchParam()
{
this.NorthEastPoint=新映射点();
西南点=新地图点();
CurrentLocation=新映射点();
MaximumSearchRadius=int.Parse(ConfigurationManager.AppSettings[“MaxDistanceBetweenPoints”]);
}
///
///获取或设置西北点。
///
公共地图点东北点{get;set;}
///
///获取或设置东南点。
///
公共地图点西南点{get;set;}
///
///获取或设置当前位置。
///
公共映射点当前位置{get;set;}
///
///获取或设置最大搜索半径。
///
公共int最大搜索半径{get;内部集;}
///
///获取或设置一个值,该值指示是否包含完整图像。
///
公共bool IncludeFullImage{get;set;}
}
MapPoint定义为:
public class BoxSearchParam
{
/// <summary>
/// Initializes a new instance of the <see cref="BoxSearchParam"/> class.
/// </summary>
public BoxSearchParam()
{
this.NorthEastPoint = new MapPoint();
SouthWestPoint = new MapPoint();
CurrentLocation = new MapPoint();
MaximumSearchRadius = int.Parse(ConfigurationManager.AppSettings["MaxDistanceBetweenPoints"]);
}
/// <summary>
/// Gets or sets the north west point.
/// </summary>
public MapPoint NorthEastPoint { get; set; }
/// <summary>
/// Gets or sets the south east point.
/// </summary>
public MapPoint SouthWestPoint { get; set; }
/// <summary>
/// Gets or sets the current location.
/// </summary>
public MapPoint CurrentLocation { get; set; }
/// <summary>
/// Gets or sets the maximum search radius.
/// </summary>
public int MaximumSearchRadius { get; internal set; }
/// <summary>
/// Gets or sets a value indicating whether include full image.
/// </summary>
public bool IncludeFullImage { get; set; }
}
public class MapPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="MapPoint"/> class.
/// </summary>
public MapPoint()
{
Londitude = 0;
Latitude = 0;
}
/// <summary>
/// Gets or sets the londitude.
/// </summary>
public decimal Londitude { get; set; }
/// <summary>
/// Gets or sets the latitude.
/// </summary>
public decimal Latitude { get; set; }
}
公共类映射点
{
///
///初始化类的新实例。
///
公共地图点()
{
Londitude=0;
纬度=0;
}
///
///获取或设置londitude。
///
公共十进制Londitude{get;set;}
///
///获取或设置纬度。
///
公共十进制纬度{get;set;}
}
我在发布数据时哪里出了问题,从我所看到的情况来看,我已经复制了javascript帖子中的功能,因此我不知道它为什么不起作用。更改行
[dict setObject:@“false”forKey:@“IncludeFullImage”]代码>
到
[dict[NSNumber number Withbool:NO]forKey:@“IncludeFullImage”]
然后再试一次。希望对您有所帮助。在mac电脑上试用charles proxy,看看到底是什么通过了网络。谢谢,我将标记您的答案为正确,因为它使我走上了正确的道路。我需要的是:[dict setObject:[NSNumber numberWithBool:NO]forKey:@“IncludeFullImage”];然而,即使这样,我仍然会经历同样的行为,最终解决的办法是将请求setValue:@“application/json”forHTTPHeaderField:@“Content Type”]添加到实际请求中。