我有mvc5网站和有问题的编码链接。
string text = "Guns N' Roses";
string Link1 = Url.Action("Artist", "Index", new { id = text }); // -> Index/Artist/Guns%20N'%20Roses
string Link2 = "Artist/Index/" + Url.Encode(text); // -> Artist/Index/Guns+N%27+Roses
string Link3 = Url.Action("Artist", "Index", new { id = Url.Encode(text) }); // -> /Index/Artist/Guns%2bN%2527%2bRoses对于链接,我使用Url.Action,它路由params和返回链接。在这个例子中,它返回的是“"Guns%20N'%20Roses","Guns+N%27+Roses". Guns N‘Roses”,但是当我只是通过Url.Encode隐秘的param时,它返回了“Guns+N%27+Roses”.。
他们怎么会不一样呢?如何强迫Url.Action使仿射像Url.Encode?因为如果我在Url.Encode中使用wrap,然后用路由创建链接,就会进行双重编码,这是不正确的。
那么如何使"Artist/Index/Guns+N%27+Roses"?这样的Url.Action返回链接
杜克
发布于 2017-08-12 16:38:32
您可以像下面这样组合Url.Action()和Url.Encode()。
string text = "Guns N' Roses";
Url.Action("Artist", "Index") + "/" + Url.Encode(text);或
string text = "Guns N' Roses";
string.Format("{0}/{1}",Url.Action("Artist", "Index"), Url.Encode(text));https://stackoverflow.com/questions/45641723
复制相似问题