gpt4 book ai didi

javascript - '&' symbol got escaped in the url when used with ' 窗口位置'

转载 作者:行者123 更新时间:2023-12-02 21:00:46 25 4
gpt4 key购买 nike

在我的 mvc 应用程序内的 View 页面中,我有这样的内容

@{ 
var controllerUrl = Url.Content("~/" + ViewBag.NextLink);
var fullPath = controllerUrl + "?stdId=4044&stdPtId=3052&=5366";
}

<script type="text/javascript">
var fullPath = '@fullPath';
</script>

<div class="marginspace">
<input type="submit" class="btn btn-primary pull-right" value="Next" onclick="window.location=fullPath;" />
</div>

问题是当我单击“下一步”按钮时,网址显示

http://something?stdId=4044&amp;stdPtId=3052&amp;fid=5366

查看网址中的“&”符号是如何转义的

我期望的网址应该是

http://something?stdId=4044&stdPtId=3052&fid=5366

那么如何确保正确构建网址而不转义“&”符号?

顺便说一句,我已经尝试过这个

onclick="window.location=decodeURI(fullPath);"

还有这个

onclick="window.location=unescape(fullPath);"

仍然没有任何改变。

最佳答案

问题出在这一行:

 var fullPath = '@fullPath';
默认情况下,Razor @ 将对您的输出进行 HTML 编码。由于 & 不是有效的 HTML,因此它将其转换为 &。这很棒,因为它意味着您不需要对所有输入进行健全性检查,只需 @User.CompanyName(例如名称为“B&Q”)即可输出 B&Q 显示为 B&Q

有时您希望输出完全按原样而不进行编码,您可以使用:

var fullPath = '@Html.Raw(fullPath)';

它将按原样输出变量,而无需先对其进行编码。

关于javascript - '&' symbol got escaped in the url when used with ' 窗口位置',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61361147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com