ASP.NET MVC Javascript 重定向编码问题
我正在尝试使用 Razor 将 URL 注入 Javascript 变量,然后将浏览器重定向到该 URL。我需要这样做,因为我的应用程序完全基于 AJAX,并且我正在重定向到 Facebook 身份验证页面。
执行客户端重定向的视图如下所示:
@model Site.Core.ViewModels.Auth.FacebookCanvasLoginModel
<script type="text/javascript">
var redirectUrl = '@(Model.FacebookLoginUrl)';
window.top.location.href = redirectUrl;
</script>
Model.FacebookLogin URL 的值由 MVC 中的 Url.Action() 帮助器生成。
问题是实际重定向到的位置是:
http://dev.mydomain.net/Auth/RequestPermission?permissions=read_stream%2Cuser_photos&redirectUrl=http%3A%2F%2Fdev.mydomain.net%2Fnewsfeed%3F_%3D1328276903643
其中包含 &redirectUrl=
而不是 &redirectUrl=
我如何将此 URL 传递到 window. location.href 没有将 &
编码为 &
,同时将redirectUrl 保持为编码的 URL 字符串?
I'm trying to use Razor to inject a URL into a Javascript variable, and then redirect the browser to that URL. I need to do it this way as my application is fully AJAX based and I'm redirecting to a Facebook Authentication page.
The view that performs the client-side redirect looks like this:
@model Site.Core.ViewModels.Auth.FacebookCanvasLoginModel
<script type="text/javascript">
var redirectUrl = '@(Model.FacebookLoginUrl)';
window.top.location.href = redirectUrl;
</script>
The value of Model.FacebookLogin URL is generated by the Url.Action() helper in MVC.
the problem is that the location that is actually redirected to is:
http://dev.mydomain.net/Auth/RequestPermission?permissions=read_stream%2Cuser_photos&redirectUrl=http%3A%2F%2Fdev.mydomain.net%2Fnewsfeed%3F_%3D1328276903643
which contains &redirectUrl=
instead of &redirectUrl=
How do I pass this URL to window.location.href without the &
being encoded to &
whilst maintaining the redirectUrl as an encoded URL string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取原始值,以便它不会对该值进行 html 编码。
你将它放入 javascript 中,并且你会想要转义任何可能使你的 javascript 出错的东西。所以试试这个:
供您参考:Json 类
You need to get the Raw value so that it doesn't html encode the value.
You are putting it inside javascript, and you'll want to escape anything that could goof up your javascript. So try this:
For your reference: Json class