asp.net-mvc Url.Action with RouteValueDictionary with Protocol

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15433161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:39:12  来源:igfitidea点击:

Url.Action with RouteValueDictionary with Protocol

asp.net-mvc

提问by Rob Cooper

been looking at this for a while, and feel like I am just being stupid want to get some more eyes on it..

看这个已经有一段时间了,觉得我只是很愚蠢,想多看看它..

I need to generate a full URL, (e.g. http://www.domain.com/controller/action?a=1&b=2), generally I just use Url.Actionto do this with no problems by specifying the protocol:

我需要生成一个完整的 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");

I've started putting together a class that returns a RouteValueDictionaryto make these anonymous objects disappear. However, I can't get it to work alongside the helper.

我已经开始组合一个返回 a 的类,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"

Any pointers that lead to a facepalm greatly appreciated :)

任何导致面掌的指针都非常感谢:)

Update:

更新:

It's probably best to clarify that in the above sample, I need to get the 'X' variable working correctly, since the RouteValueDictionary is created elsewhere in code. Assume that the RouteValueDictionary is correct.

最好澄清一下,在上面的示例中,我需要让 ' X' 变量正常工作,因为 RouteValueDictionary 是在代码中的其他地方创建的。假设 RouteValueDictionary 是正确的。

I just don't understand why this works with anonymous object, but the same object wrapped in the same object wrapped in a RouteValueDictionarymakes the helper freak out.

我只是不明白为什么这适用于匿名对象,但是包裹在包裹在同一个对象中的同一个对象RouteValueDictionary让助手吓坏了。

采纳答案by Oliver

The overload that you are using expects type "object" for the parameter where you are passing the RouteValueDictionary. For some reason this is causing the issue, maybe something to do with .ToString()? Use an overload that accepts a RouteValueDictionaryand this should work.

您正在使用的重载需要您传递RouteValueDictionary. 出于某种原因,这导致了这个问题,也许与 .ToString() 有关系?使用接受 a 的重载,RouteValueDictionary这应该可以工作。

To test this, add a hostName argument to select the overload shown below:

要对此进行测试,请添加一个 hostName 参数以选择如下所示的重载:

Correct overload

正确的过载

EDIT

编辑

You could use this extension in your project to add the overload that you require to Url.Action. Internally it will resolve and add the hostName from the request.

你可以在你的项目中使用这个扩展来向 Url.Action 添加你需要的重载。在内部,它将解析并添加请求中的主机名。

public static string Action
    (this UrlHelper helper, string action, 
     string controller, RouteValueDictionary routeValues, string protocol)
{
     string hostName = helper.RequestContext.HttpContext.Request.Url.Host;
     return helper.Action(action, controller, routeValues, protocol, hostName);
}

回答by DavidWhitney

Interestingly, it looks like your specific example is matching a method signature that takes "object" as a property, rather than RouteValueDictionary. As such, it's just ToString()ing out the typename, rather than correctly serializing a RouteValueDictionary

有趣的是,看起来您的具体示例正在匹配将“对象”作为属性而不是 RouteValueDictionary 的方法签名。因此,它只是 ToString() 输出类型名,而不是正确序列化 RouteValueDictionary

 var x = Url.Action("Index", "Home", new RouteValueDictionary(new { a = 1, b = 2 }), "http", string.Empty);

Note "string.Empty"at the end.

注意末尾的“string.Empty”

That's enough to force the code to use a different overload, that accepts a RouteValueDictionary and as such, serializes correctly.

这足以强制代码使用不同的重载,它接受 RouteValueDictionary 并因此正确序列化。

// http://localhost:55110/?a=1&b=2