MVC扩展Url.Action方法解决复杂对象参数问题
2024-10-19 18:34:58
1:问题描述
@Url.Action("Index", "Home", new { Key = "Key", Val = new { Name = "TypeDescriptor" } })
期望结果: /Home/Index?Key=Key&Name=TypeDescriptor
实际结果: /Home/Index?Key=Key
2.解决方案
/// <summary>
/// 递归算法
/// </summary>
/// <param name="dictionary">dictionary</param>
/// <param name="values">values</param>
public static void GetProperties(RouteValueDictionary dictionary, object values)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
{
object obj = descriptor.GetValue(values);
if (obj != null)
{
Type type = obj.GetType();
if (!type.IsValueType && type != typeof(string))
{
GetProperties(dictionary, obj);
}
if (type.IsValueType || type == typeof(string))
{
if (dictionary.ContainsKey(descriptor.Name))
{
dictionary[descriptor.Name] = obj;
}
else
{
dictionary.Add(descriptor.Name, obj);
}
}
}
}
}
递归算法获取参数列表
/// <summary>
/// Action
/// </summary>
/// <param name="urlHelper">urlHelper</param>
/// <param name="actionName">actionName</param>
/// <param name="controllerName">controllerName</param>
/// <param name="routeValues">routeValues</param>
/// <returns>string</returns>
public static string Action(this UrlHelper urlHelper, string actionName, string controllerName, object routeValues)
{
RouteValueDictionary dictionary = new RouteValueDictionary();
GetProperties(dictionary, routeValues);
return UrlHelper.GenerateUrl(null, actionName, controllerName, dictionary, urlHelper.RouteCollection, urlHelper.RequestContext, true);
}
重写Action
最新文章
- javascript面向对象系列第四篇——选项卡的实现
- js 基础
- 23. Can&#39;t connect to X11 window server using &#39;127.0.0.1:0.0&#39; as the value of the DISPLAY variable.解决办法
- ASP.NET MVC 网站开发总结(二)——一个或多个文件的异步或同步上传
- Oracle执行语句跟踪(2)——使用10046事件实现语句追踪
- wing IDE破解方法
- struct TABLE_SHARE
- jquery ajax 跨域处理
- linux安装php5.3
- android可扩展自己的定义,运动图像裁剪框
- Linux 上搭建 git 的服务器
- 51 nod 1394 1394 差和问题(线段树)
- 关于ROS学习的一些反思
- python模拟登陆Github示例
- [转载]关于laravel中表关系的一对一、一对多、多对一、多对多实践
- (PMP)第3章-----项目经理的角色
- 利用ArcGIS-Server瓦片制作离线地图包(*.tpk)_详细流程
- ABBYY FineReader 12使用教程
- go语言练习:条件语句和循环语句
- 中文解码Unicode