MVC 自定义IModelBinder实现json参数转Dictionary<string, string>
IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string>
一、原始json转Dictionary<string, string>
如果不用IModelBinder,那么将Json字符串转换为Dictionary<string, string>的代码写法是:
/// <summary>
/// 手动Json转Dictionary
/// </summary>
/// <param name="Param"></param>
/// <returns></returns>
public ActionResult ManualJsonToDictionary(string Param)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
//将Json字符串转换为Dictionary<string, string>
Dictionary<string, string> DicParam = jss.Deserialize<Dictionary<string, string>>(Param);
return Content(DicParam["Title"]);
}
当这个需求多了之后,这种代码就太多了,可以通过实现IModelBinder来简化。
二、实现IModelBinder简化
简化后的代码如下:
/// <summary>
/// 测试IModelBinder接口的实现
/// </summary>
/// <param name="DicParam"></param>
/// <returns></returns>
public ActionResult TestJsonToDictionary([ModelBinder(typeof(JsonToDictionaryBinder))]Dictionary<string, string> DicParam)
{
return Content(DicParam["Title"]);
}
其中实现IMidelBinder的类JsonToDictionaryBinder的代码如下:
/// <summary>
/// Json转Dictionary<string, string>
/// </summary>
public class JsonToDictionaryBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Dictionary<string, string> DicParam = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(Convert.ToString(controllerContext.HttpContext.Request["Param"])))
{
System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
DicParam = sr.Deserialize(controllerContext.HttpContext.Request["Param"], typeof(Dictionary<string, string>)) as Dictionary<string, string>;
}
return DicParam;
}
}
前台测试页面代码如下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function(){
var jsonStr = "{\"Id\":\"1\",\"Title\":\"张飞\"}";
$.ajax({
url: "/Home/TestJsonToDictionary",
dataType: "text",
data:{
Param: jsonStr
},
success: function (response) {
alert(response);
}
}) $.ajax({
url: "/Home/ManualJsonToDictionary",
dataType: "text",
data: {
Param: jsonStr
},
success: function (response) {
alert(response);
}
})
})
</script>
</head>
<body>
<div></div>
</body>
</html>
MVC 自定义IModelBinder实现json参数转Dictionary<string, string>的更多相关文章
- [MVC] 自定义ActionSelector,根据参数选择Action
很多时候我们会根据UI传入的参数,呈现不同的View.也就是对于同一个Action如何根据请求数据返回不同的View.通常情况下我们会按照如下方法来写,例如: [AcceptVerbs(HttpVer ...
- [MVC] 自定义ActionSelector,根据参数选择Action[转载]
很多时候我们会根据UI传入的参数,呈现不同的View.也就是对于同一个Action如何根据请求数据返回不同的View.通常情况下我们会按照如下方法来写,例如: [AcceptVerbs(HttpVer ...
- Spring MVC(七)--传递JSON参数
有时候参数的传递还需要更多的参数,比如一个获取用户信息的请求中既有用户ID等基本参数,还要求对查询结果进行分页,针对这种场景,一般都会将分页参数封装成一个对象,然后将它和基本参数一起传给控制器,为了控 ...
- SpringBoot:自定义注解实现后台接收Json参数
0.需求 在实际的开发过程中,服务间调用一般使用Json传参的模式,SpringBoot项目无法使用@RequestParam接收Json传参 只有@RequestBody支持Json,但是每次为了一 ...
- Spring boot中自定义Json参数解析器
转载请注明出处... 一.介绍 用过springMVC/spring boot的都清楚,在controller层接受参数,常用的都是两种接受方式,如下 /** * 请求路径 http://127.0. ...
- mvc自定义全局异常处理
异常信息处理是任何网站必不可少的一个环节,怎么有效显示,记录,传递异常信息又成为重中之重的问题.本篇将基于上篇介绍的html2cancas截图功能,实现mvc自定义全局异常处理.先看一下最终实现效果: ...
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List转JSON,JSON转List,JSON转C#对象
一.JSON解析与字符串化 JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 ...
- ASP.NET MVC 自定义路由中几个需要注意的小细节
本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...
- Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...
随机推荐
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
- VC++ 文件系统
using namespace System; using namespace System::IO; void ShowHelpMsg(){ Console::WriteLine(L"本程 ...
- 在线工具、setHtmlRem、px2rem
http://tool.lu/c/developer 开发类在线工具 https://github.com/leon776/setHtmlRem setHtmlRem https://githu ...
- IOS开发-当遇到tableView整体上移时的解决方案
方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...
- git error
一,今天在上传代码时出错: $ git push -u origin mastererror: The requested URL returned error: 403 Forbidden whil ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- iOS 常用设计模式和机制之KVO
KVO http://blog.kyleduo.com/2014/10/20/ios_learning-kvo/ 定义: KVO:Key-Value Observing,是Foundation框架提供 ...
- hive索引表
create table index_tmp(id int,name string,dt string) row format delimited fields terminated by ',' s ...
- 论文笔记之:A CNN Cascade for Landmark Guided Semantic Part Segmentation
A CNN Cascade for Landmark Guided Semantic Part Segmentation ECCV 2016 摘要:本文提出了一种 CNN cascade (CNN ...
- phpunit4.1的干净测试
一般而言,写测试时需要加载一些文件来进行自动加载 但在phpunit4.1中只要其中一个测试文件加载了,其他测试文件就不需要再加载