重定向从javascript行动 [英] Redirecting to action from javascript

查看:120
本文介绍了重定向从javascript行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC4项目,并在客户端我需要重定向到一个具体的操作方法。我已阅读下列职位<一个href=\"http://stackoverflow.com/questions/5336858/how-to-redirect-to-action-from-javascript-method\">How从JavaScript方法重定向到行动,并进一步下跌有关于使用注释:

I have a MVC4 project, and on the client side I need to redirect to a specific action method. I have read the following post How to redirect to action from JavaScript method?, and further down there's a comment about using:

window.location.href = "/{controller}/{action}/{params}";

和我曾尝试已,但它不工作在我的项目是安装在IIS。

and I had tried that already but it's not working for where my project is installed in IIS.

我的项目已发布到: HTTP://localhost/SomeName.SomeOtherName/

和我需要去: HTTP://localhost/SomeName.SomeOtherName/Home/Logout

当我使用'/控制器/行动从previous后我到这里推荐:本地主机/主页/注销,这是不是正确。

When I use the '/Controller/Action' as recommended from the previous post I get to here: localhost/Home/Logout and this isn't correct.

我试图保持发布的位置(同上面发布字符串)在web.config文件并生成字符串(连击:发布位置+'/主页/注销'),但没有任何工作。下面是我使用这个语句。什么是奇怪的是,这只是地连接了当前页面的网址与我建立的网址。这不仅是无效的,但我也得到其中的一个潜在危险Request的...的错误。

I've tried to keep the published location (same as publish string above) in a web.config file and build the string (concatenate: publish location + '/Home/logout'), but that didn't work either. Below is the stmt that I'm using for this. What's weird is that this just concatenates the current page's url with my built url. Not only is this invalid, but I also get one of those 'potentially dangerous request.path...' errors.

 window.location.href = "\"" + url + "/Home/logout" + "\"";

如果我使用$(location.hostname)来构建我的字符串同样的事情发生。

The same thing happens if I use $(location.hostname) to build my string.

有什么想法?

推荐答案

生成URL到一个控制器动作时总是使用网址帮手。例如,如果你的脚本重定向位于你可以这样做的视图中:

Always use url helpers when generating urls to a controller action. For example if your script that redirects is located inside the view you could do this:

<script type="text/javascript">
    window.location.href = '@Url.Action("LogOut", "Home")';
</script>

Url.Action 服务器端的助手将在这里确保正确的URL生成,无论您的应用程序是否在虚拟目录内承载与否。

The Url.Action server side helper will ensure here that the proper url is generated no matter whether your application is hosted inside a virtual directory or not.

如果在其他情况下,JavaScript是位于一个单独的js文件(这显然是最好的做法),你不能使用服务器端的助手,那么你有几个可能的原因:

If on the other case your javascript is located in a separate js file (which obviously is the best practice) where you cannot use server side helpers, then you have a couple of possibilities:


  • 使用一个全局JavaScript变量是这样的视图内计算和你的脚本中使用:

  • Use a global javascript variable that's calculated inside the view and used by your script:

<script type="text/javascript">
    var logoutUrl = '@Url.Action("LogOut", "Home")';
</script>

然后某处你的脚本里面:

and then somewhere inside your script:

window.location.href = logoutUrl;


  • 使用HTML5数据 - *属性,不知怎的,将与用户的的退出的相关的一些DOM元素上。例如,可能会有一些格或一个按钮:

  • Use HTML5 data-* attribute on some DOM element that somehow will be associated with the logging out of an user. For example that could be some div or a button:

    <div id="logout" data-url="@Url.Action("LogOut", "Home")">Log out</div>
    

    ,然后你的脚本里面:

    and then inside your script:

    $('#logout').click(function() {
        window.location.href = $(this).data('url');
    });
    

    显然,这似乎是一个愚蠢而过于简单的例子,因为在这种特殊情况下,你也只会使用了 Html.ActionLink 来生成一个锚注销而没有担心任何JavaScript或类似的东西,但我希望它会为你提供一些例如为您真实的场景。

    Obviously this seems like a stupid and oversimplified example, because in this particular case you would simply have used an Html.ActionLink to generate a LogOut anchor without ever worrying about any javascript or things like that, but hopefully it would provide you with some example for your real world scenario.

    请注意,在所有的例子的网址助手用来生成正确的URL到控制器的行动,并考虑到任何可能的虚拟目录。这是最重要的规则:绝不会硬code在ASP.NET MVC应用程序的网址

    Notice how in all examples an url helper is used to generate the proper url to the controller action and take into account any possible virtual directories. That's the most important rule: never hardcode an url in an ASP.NET MVC application.

    这篇关于重定向从javascript行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆