我有一个类
class Example
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public String Prop3 { get; set; }
}
我如何的自动地的转换这个对象为urlen codeD字符串,然后将其添加到我的主机?
How can I automagically convert this object to a urlencoded string and then append it to my hostname?
Urlen codeD字符串:
为prop1 = VAL1和放大器; prop2 = val2的&放大器; prop3 = VAL3
Urlencoded string:
prop1=val1&prop2=val2&prop3=val3
最终结果:
http://example.com?prop1=val1&prop2=val2&prop3=val3
Final result:
http://example.com?prop1=val1&prop2=val2&prop3=val3
您可以使用UrlHelper:
var model = new MyClass
{
Prop1 = 1,
Prop2 = 2,
Prop3 = "prop 3"
};
string url = Url.Action("index", "home", model);
// will generate /?Prop1=1&Prop2=2&Prop3=prop%203
如果你想要一个绝对URL只是使用 适当超载
:
string url = Url.Action("index", "home", model, "http");