JSON从前端获取数据,传值给后端,
1 public ActionResult Index(string logType, string requestType, string ip, DateTime? startTime, DateTime? endTime, string pIndex, string pSize) 2 //其中string logType, string requestType, string ip, DateTime? startTime, DateTime? endTime, string pIndex, string pSize是从前端传来的JSON数据 3 { 4 var result = QueryLogsByCondition(null, logType, requestType, ip, null, null, null, null, null, null, null, (DateTime)startTime, (DateTime)endTime, pageIndex, pageSize).Data; 5 //... 6 }
如果都改成null,这是肯定可以的,但现在要传值
1 public ThirdOpenAPIResponseDataPageInfo<LogModel> QueryLogsByCondition(Guid? channelUID, int? logType, int? requestType, string ip, string localIP, string api, string controller, string action, string requestBody, string title, string subTitle, DateTime startTime, DateTime endTime, int? pageIndex, int? pageSize) 2 { 3 var task = Task.Run(() => ThirdOpenAPIService.PostHttp<ThirdOpenAPIResponseDataPageInfo<LogModel>>(ThirdOpenAPIService.RequestUri(ApiUrls.Log.queryLogsByCondition), new { channelUID= channelUID,logType = logType, requestType = requestType, ip = ip, localIP= localIP, api= api, controller= controller, action= action, requestBody= requestBody, title= title, subTitle= subTitle,startTime = startTime, endTime = endTime, pageIndex = pageIndex, pageSize = pageSize })).Result; 4 return task; 5 }
在QueryLogsByCondition方法中,logType和requestType都是int类型,所以假如用最上面的代码,logType和requestType则会报红,如果改成int.Parse(),如下
1 var result = QueryLogsByCondition(null, int.Parse(logType), int.Parse(requestType), ip, null, null, null, null, null, null, null, (DateTime)startTime, (DateTime)endTime, pageIndex, pageSize).Data;
则会报错(因为第一次访问时,并没有传值过去),错误如下
[ArgumentNullException: 值不能为 null。 参数名: String]
请问这到底应该怎么解决?
logType, requestType 先判断是不是null, 如果是null,给个0或者默认值