admin管理员组文章数量:1428258
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:
;amp;redirectUrl=http%3A%2F%2Fdev.mydomain%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?
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/Auth/RequestPermission?permissions=read_stream%2Cuser_photos&redirectUrl=http%3A%2F%2Fdev.mydomain%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?
-
1
What's the code within the
FacebookLoginUrl
method? What's it returning? It seems like this method is the issue and is adding the encoding when it shouldn't. (By the way, for argument's sake, having<a href="http://site./?foo&bar">
with the&
is the proper way of generating a URL.) – Brad Christie Commented Feb 3, 2012 at 14:46
1 Answer
Reset to default 8You need to get the Raw value so that it doesn't html encode the value.
@Html.Raw(Model.FacebookLoginUrl)
You are putting it inside javascript, and you'll want to escape anything that could goof up your javascript. So try this:
var redirectUrl = @Html.Raw(Json.Encode(Model.FacebookLoginUrl))
For your reference: Json class
本文标签: ASPNET MVC Javascript Redirect encoding issueStack Overflow
版权声明:本文标题:ASP.NET MVC Javascript Redirect encoding issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745523089a2661716.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论