信息发布系统 Jquery+MVC架构开发(7) Controller层

本文深入探讨了在.NET环境下如何利用WCF实现远程调用,并结合JSON绑定技术,通过自定义JSONBinder实现复杂数据类型的解析。详细介绍了如何创建控制器、配置JSON绑定器、以及实现对信息的获取、查询、添加、更新和删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller 这一层首先要添加对WCF 的引用:

如下,输入我们自己的wcf地址

http://localhost:8732/Design_Time_Addresses/InfoPub.BLLService/Service1/mex

为了解析嵌套结构的类,我们加入JsonBinder

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Script.Serialization;

namespace InfoPub.Controllers

{

public class JsonBinder<T> : IModelBinder

{

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)

{

// 从䨮请?求¨®中D获?取¨?提¬¨¢交?的Ì?参?数ºy数ºy据Y

var json = controllerContext.HttpContext.Request.Form[bindingContext.ModelName] as string;

// 提¬¨¢交?参?数ºy是º?对?象¨®

if (json.StartsWith("{") && json.EndsWith("}"))

{

JavaScriptSerializer js = new JavaScriptSerializer();

object obj = js.Deserialize<T>(json);

return obj;

}

// 提¬¨¢交?参?数ºy是º?数ºy组Á¨¦

if (json.StartsWith("[") && json.EndsWith("]"))

{

JavaScriptSerializer js = new JavaScriptSerializer();

List<T> obj = js.Deserialize<List<T>>(json);

return obj;

}

return null;

}

}

}

我们依次添加三个controller,Infocontroller,InfoTypeContrller,UserInfoContrller,如下:

注意我们添加空的controller即可,别的controller我们暂用不到,如下:

下面我们添加Controller方法,于InfoController为例说明:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using InfoPub.InfoPubService;

namespace InfoPub.Controllers

{

public class InfoController : Controller

{

private InfoPubServiceClient infoPubService = new InfoPubServiceClient();

public JsonResult GetInfoList([ModelBinder(typeof(JsonBinder<SearchInfo>))]SearchInfo searchInfo)

{

InfoList infoList = new InfoList();

infoList = infoPubService.GetInfoList(searchInfo);

if (infoList.infoResult.Code != 0)

{

return Json(new { Data = infoList, isSuccess = false, message = "GetInfoList fail ", errorCode = infoList.infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);

}

return Json(new { Data = infoList, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);

}

public JsonResult GetInfoById(int infoId)

{

InfoList infoList = new InfoList();

infoList = infoPubService.GetInfoById(infoId);

if (infoList.infoResult.Code != 0)

{

return Json(new { Data = infoList, isSuccess = false, message = "GetInfoById fail ", errorCode = infoList.infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);

}

return Json(new { Data = infoList, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);

}

public JsonResult AddInfo(Info info)

{

InfoResult infoResult = new InfoResult();

infoResult = infoPubService.AddInfo(info);

if (infoResult.Code != 0)

{

return Json(new { Data = infoResult, isSuccess = false, message = "AddInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);

}

return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);

}

public JsonResult UpdateInfo(Info info)

{

InfoResult infoResult = new InfoResult();

infoResult = infoPubService.UpdateInfo(info);

if (infoResult.Code != 0)

{

return Json(new { Data = infoResult, isSuccess = false, message = "UpdateInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);

}

return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);

}

public JsonResult DeleteInfo(int infoId)

{

InfoResult infoResult = new InfoResult();

infoResult = infoPubService.DeleteInfo(infoId);

if (infoResult.Code != 0)

{

return Json(new { Data = infoResult, isSuccess = false, message = "DeleteInfo fail ", errorCode = infoResult.Code }, "text/json", JsonRequestBehavior.AllowGet);

}

return Json(new { Data = infoResult, isSuccess = true }, "text/json", JsonRequestBehavior.AllowGet);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值