简体   繁体   English

从javascript重定向到操作

[英]Redirecting to action from javascript

I have a MVC4 project, and on the client side I need to redirect to a specific action method. 我有一个MVC4项目,在客户端我需要重定向到一个特定的操作方法。 I have read the following post How to redirect to action from JavaScript method? 我已阅读以下帖子如何从JavaScript方法重定向到操作? , and further down there's a comment about using: ,还有一个关于使用的评论:

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

and I had tried that already but it's not working for where my project is installed in IIS. 我已经尝试过了,但是我的项目安装在IIS中的位置不起作用。

My project has been published to: http://localhost/SomeName.SomeOtherName/ 我的项目已发布到: http://localhost/SomeName.SomeOtherName/

and I need to get to: http://localhost/SomeName.SomeOtherName/Home/Logout 我需要访问: http://localhost/SomeName.SomeOtherName/Home/Logout

When I use the '/Controller/Action' as recommended from the previous post I get to here: localhost/Home/Logout and this isn't correct. 当我使用上一篇文章中推荐的'/ Controller / Action'时,我到达这里: localhost/Home/Logout ,这是不正确的。

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. 我试图在web.config文件中保留已发布的位置(与上面的发布字符串相同)并构建字符串(连接:发布位置+'/ Home / logout'),但这也不起作用。 Below is the stmt that I'm using for this. 以下是我正在使用的stmt。 What's weird is that this just concatenates the current page's url with my built url. 奇怪的是,这只是将当前页面的url与我构建的url连接起来。 Not only is this invalid, but I also get one of those 'potentially dangerous request.path...' errors. 这不仅无效,而且我还得到其中一个“潜在危险的request.path ...”错误。

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

The same thing happens if I use $(location.hostname) to build my string. 如果我使用$(location.hostname)来构建我的字符串,会发生同样的事情。

Any thoughts? 有什么想法吗?

Always use url helpers when generating urls to a controller action. 在生成控制器操作的URL时,始终使用url帮助程序。 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>

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. 无论您的应用程序是否托管在虚拟目录中, Url.Action服务器端帮助程序都将确保生成正确的URL。

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位于一个单独的js文件(这显然是最好的做法),你不能使用服务器端助手,那么你有几种可能性:

  • Use a global javascript variable that's calculated inside the view and used by your script: 使用在视图中计算并由脚本使用的全局javascript变量:

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

    and then somewhere inside your script: 然后在你的脚本里面的某个地方:

     window.location.href = logoutUrl; 
  • Use HTML5 data-* attribute on some DOM element that somehow will be associated with the logging out of an user. 在某些DOM元素上使用HTML5 data- *属性,该元素将以某种方式与注销用户相关联。 For example that could be some div or a button: 例如,可能是一些div或按钮:

     <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'); }); 

    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. 显然这似乎是一个愚蠢和过于简单的例子,因为在这种特殊情况下你只需使用Html.ActionLink生成一个LogOut锚,而不必担心任何javascript或类似的东西,但希望它会为你提供一些示例你真实的场景。

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. 请注意,在所有示例中,如何使用url帮助程序为控制器操作生成正确的URL,并考虑任何可能的虚拟目录。 That's the most important rule: never hardcode an url in an ASP.NET MVC application. 这是最重要的规则: 永远不要在ASP.NET MVC应用程序中硬编码。

While this might seem random, this did involve the above situation because everything was appearing in a dynamic popup. 虽然这似乎是随机的,但这确实涉及上述情况,因为一切都出现在动态弹出窗口中。 There are 2 buttons (logout, continue) and an image. 有2个按钮(注销,继续)和图像。 I was having trouble getting the image to appear correctly, and wanted a dynamic link using helpers. 我无法正确显示图像,并希望使用帮助程序进行动态链接。 I solved the issue using the following code: 我使用以下代码解决了这个问题:

  var siteRoot = "@(Request.Url.Scheme)://@(Request.Url.Host)@(Url.Content("~"))"; 

Anyone see anything wrong with this? 有人看到这个有什么不对吗? All other URL helper examples always included the controller and method and I needed a valid, complete root value. 所有其他URL帮助程序示例始终包含控制器和方法,我需要一个有效的完整根值。 Thoughts? 思考?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM