webapi 接口规则

时间:2023-03-09 03:43:59
webapi  接口规则
  [HttpPost]
[AuthorizeFilter]
public HttpResponseMessage DeleteStudentInfo([FromBody] object value)
{
ApiResult<StudentEntity> res = new ApiResult<StudentEntity>();
try
{
StudentEntity model = JsonConvert.DeserializeObject<StudentEntity>(JsonConvert.SerializeObject(value));
if (model != null && model.id > )
{
model.IsDelete = ;
StudentLogic bll = new StudentLogic();
int result = ;
result = bll.UpdateCommand(model);
if (result > )
{
res.ResultFlag = ;
res.ResultMsg = "操作成功";
res.ResultObj = null;
}
else
{
res.ResultFlag = ;
res.ResultMsg = "操作失败";
res.ResultObj = null;
}
}
else
{
res.ResultFlag = ;
res.ResultMsg = "参数错误";
res.ResultObj = null;
}
}
catch (Exception ex)
{
res.ResultFlag = ;
res.ResultMsg = ex.Message;
res.ResultObj = null;
//写错误日志
WebLogTool.WriteLog(ex, "StudentController-DeleteStudentInfo");
}
return HttpHelper.ResponseMessagetoJson(res);
}

json 处理

#region 程序集 Newtonsoft.Json.dll, v4.5.0.0
// D:\wanglei\Wisdom.JPClient\trunk\Code\Wisdom.JPClient.WebApi\bin\Newtonsoft.Json.dll
#endregion

 public class ApiResult<T>
{
int _ResultFlag;
/// <summary>
/// 接口调用状态(1成功 0系统错误 其余状态自定义)
/// </summary>
public int ResultFlag
{
get { return _ResultFlag; }
set { _ResultFlag = value; }
}
string _ResultMsg;
/// <summary>
/// 接口返回消息
/// </summary>
public string ResultMsg
{
get { return _ResultMsg; }
set { _ResultMsg = value; }
}
T _ResultObj;
/// <summary>
/// 接口返回对象
/// </summary>
public T ResultObj
{
get { return _ResultObj; }
set { _ResultObj = value; }
}
} /// <summary>
/// Web Api 返回对象
/// </summary>
public class ApiResult_<T>
{
string _ResultFlag;
/// <summary>
/// 接口调用状态(1成功 0系统错误 其余状态自定义)
/// </summary>
public string ResultFlag
{
get { return _ResultFlag; }
set { _ResultFlag = value; }
}
string _ResultMsg;
/// <summary>
/// 接口返回消息
/// </summary>
public string ResultMsg
{
get { return _ResultMsg; }
set { _ResultMsg = value; }
}
T _ResultObj;
/// <summary>
/// 接口返回对象
/// </summary>
public T ResultObj
{
get { return _ResultObj; }
set { _ResultObj = value; }
}
}

api result 类

 public void DgUserDataBind()
{
if (pagerUser.PageIndex == )
{
pagerUser.PageSize = ;//默认值20 自定义的时候需要传
} //查询条件
Hashtable ht = new Hashtable();
//if (!string.IsNullOrEmpty(txtStuNo.Text.Trim()))//学员编号
//{
// ht["StuNo"] = txtStuNo.Text.Trim();
//} //if (cboClass.SelectedIndex > 0)//班级
//{
// ht["Class"] = cboClass.SelectedValue.ToString();
//} if (cboAppointmentStatus.SelectedIndex > )//预约状态
{
ht["AppointmentStatus"] = cboAppointmentStatus.SelectedValue.ToString();
}
//if (cboTimeQuantum.SelectedIndex > 0)//时间段
//{
// ht["TimeQuantum"] = ((CmbboxModel)cboTimeQuantum.SelectedItem).value;
//}
if (cboRegSite.SelectedIndex > )//报名点
{
ht["RegSite"] = cboRegSite.SelectedValue.ToString();
}
if (cboTrainingGround.SelectedIndex > )//训练场
{
ht["TrainingGround"] = cboTrainingGround.SelectedValue.ToString();
} if (!string.IsNullOrEmpty(cmbSchool.SelectedSchoolId))
{
ht["Schoolid"] = cmbSchool.SelectedSchoolId;
} var lstMoreCondition = ((MoreSearchConditionBar.ItemsSource as ObservableCollection<MoreSearchConditionModel>) ?? new ObservableCollection<MoreSearchConditionModel>());
var item = lstMoreCondition.Where(p => p.ConditionName == "教练员").FirstOrDefault();
if (item != null)//教练
{
ht["Coach"] = item.ConditionText.Trim();
}
item = lstMoreCondition.Where(p => p.ConditionName == "预约开始日期").FirstOrDefault();
if (item != null)//预约日期
{
ht["StartDate"] = item.ConditionText.Trim();
}
item = lstMoreCondition.Where(p => p.ConditionName == "预约结束日期").FirstOrDefault();
if (item != null)
{
ht["EndDate"] = item.ConditionText.Trim();
}
item = lstMoreCondition.Where(p => p.ConditionName == "学车分类").FirstOrDefault();
if (item != null)//学车分类
{
ht["StuCarType"] = item.ConditionText.Trim();
}
item = lstMoreCondition.Where(p => p.ConditionName == "车型").FirstOrDefault();
if (item != null)//车型
{
ht["CarType"] = item.ConditionText.Trim();//((Wisdom.JPClient.Model.CmbboxModel)(cboCarType.SelectedItem)).value;
} //初始化查询参数
PagePostParam postModel = new PagePostParam() { PageIndex = pagerUser.PageIndex, PageSize = pagerUser.PageSize, OrderBy = "id", OrderType = , ht = ht };
//调用api取值
KeyValuePair<bool, string> result = HttpHelper.PostWebRequest(BaseInfo.Cur.WebApiUrl + "StudentAppointment/getListByParam", postModel);
PageResultTable pageresult = new PageResultTable();
if (result.Key)
{
ApiResult<PageResultTable> res = JsonConvert.DeserializeObject<ApiResult<PageResultTable>>(result.Value);
if (res.ResultFlag == )
{
pageresult = res.ResultObj as PageResultTable;
grid1.ItemsSource = pageresult.dt.DefaultView;
DT = pageresult.dt;
pagerUser.TotalCount = pageresult.RowsCout;
}
else
{
MsgBox.Show(res.ResultMsg);
}
}
else
{
MsgBox.Show("操作失败!");
}
}

上面是调用端解析写法