MVC扩展Url.Action方法解决复杂对象参数问题
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
MVC扩展Url.Action方法解决复杂对象参数问题的更多相关文章
- MVC – 6.控制器 Action方法参数与返回值
6.1 Controller接收浏览器数据 a.获取Get数据 : a1:获取路由url中配置好的制定参数: 如配置好的路由: 浏览器请求路径为: /User/Modify/1 ,MVC框架获 ...
- MVC – 6.控制器 Action方法参数与返回值
6.1 Controller接收浏览器数据 a.获取Get数据 : a1:获取路由url中配置好的制定参数: 如配置好的路由: 浏览器请求路径为: /User/Modify/1 ,MVC框架获取请求后 ...
- Asp.Net MVC 扩展 Html.ImageFor 方法详解
背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...
- 用浅/深拷贝、和HTML5方法解决js对象的引用的问题
先来看一个例子 例一: var a=[1,2,3]; var b=a; b.push(4); alert(b);//1,2,3,4 alert(a);//1,2,3,4 var a=[1,2,3]; ...
- 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间
代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; u ...
- MVC教程五:Action方法的返回类型
MVC中的Action方法的返回值一般有以下几种: 类型 s说明 EmptyResult 不进行任何操作 ContentResult 将指定内容作为文本输出 JsonResult 输出JSON字符串 ...
- ASP.NET Core MVC中的IActionFilter.OnActionExecuting方法,可以获取Controller的Action方法参数值
用过ASP.NET Core MVC中IActionFilter拦截器的开发人员,都知道这是一个非常强大的MVC拦截器.最近才发现IActionFilter的OnActionExecuting方法,甚 ...
- MVC 路由URL重写
在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. URL重写与优化就是搜索引擎优化的手段之一. 假如某手机网站(基于ASP.NET MVC)分 ...
- MVC 扩展 Html.ImageFor
Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...
随机推荐
- [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- iOS UI基础-4.2应用程序管理 Xib文件使用
Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...
- sql server 获取分隔字符串后的长度
--方法1 --sql 分隔字符串,返回个数 CREATE function f_splitLen_1 ( @str varchar(1024), --要分割的字符串 @split varc ...
- Understanding Convolutional Neural Networks for NLP
When we hear about Convolutional Neural Network (CNNs), we typically think of Computer Vision. CNNs ...
- 1.hive开窗函数,分析函数
http://yugouai.iteye.com/blog/1908121 分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行.开窗函数指 ...
- ui-grid angularjs
<pre name="code" class="html"><!--ui-grid css--> <link rel=" ...
- CentOS安装mysql并配置远程访问
最近上班挺无聊,每天就是不停的重启重启重启,然后抓log.于是有事儿没事儿的看卡闲书,搞搞其他事情. 但是,公司笔记本装太多乱其八糟的东西也还是不太好. 于是,想到了我那个当VPN server的VP ...
- Python3.x(windows系统)安装matplotlib库
Python3.x(windows系统)安装matplotlib库 cmd命令: pip install matplotlib 执行结果:
- QTQuick控件基础(2)
import QtQuick 2.2import QtQuick.Controls 1.2import QtQuick.Window 2.1ApplicationWindow { visible ...
- CSAPP 第三章 读书笔记
程序的机器级表示 AT&T与Intel格式的汇编代码 我们的表述是ATT(根据"AT&T"命名的, AT&T是运营贝尔实验室多年的公 司)格式的汇编代码,这 ...