【发布时间】:2013-03-15 22:13:17
【问题描述】:
看了一段时间,觉得自己很愚蠢,想多看几眼..
我需要生成一个完整的URL,(例如http://www.domain.com/controller/action?a=1&b=2),通常我只是使用Url.Action通过指定协议来做到这一点没有问题:
var url = Url.Action("Action", "Controller", new { a = 1, b = 2 }, "http");
我已经开始组合一个返回RouteValueDictionary 的类,以使这些匿名对象消失。但是,我无法让它与助手一起工作。
var x = Url.Action("Action", "Controller", new RouteValueDictionary(new { a = 1, b = 2 }), "http");
// "http://127.0.0.1/Controller/Action?Count=2&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D",
var y = Url.Action("Action", "Controller", new { a = 1, b = 2 }, "http");
// "http://127.0.0.1/Controller/Action?a=1&b=2"
非常感谢任何导致 facepalm 的指针:)
更新:
最好澄清一下,在上面的示例中,我需要让“X”变量正常工作,因为 RouteValueDictionary 是在代码的其他位置创建的。假设 RouteValueDictionary 是正确的。
我只是不明白为什么这适用于匿名对象,但是包裹在同一对象中的同一对象包裹在 RouteValueDictionary 中让助手吓坏了。
【问题讨论】:
-
您实际上是如何创建 RouteValueDictionary 的?
标签: asp.net-mvc