IE9 从 URL 中删除 # 部分(适用于 Firefox!)

IE9 removes # part from URL (works on Firefox! )

我正在使用 ASP.NET MVC 路由 + AngularJS 路由开发一个应用程序。

我的URL长得像:

https://example.com/Request/#/Search/Request/123

当我分解时,这个 (http://example.com/Request) 由 ASP.NET MVC 路由处理。即 (Area = Request, controller = "Default", action = "Index")

(#/Search/Request/123) 由 AngularJS 路由处理。

当我在 http://localhost:8080/

时,这非常有效

问题是我将此应用程序部署到 https://example.com/

在这种情况下,如果用户点击上面的 link(通过电子邮件收到),IE 9 只识别 (https://example.com/Request/") 而服务器永远不会识别 (#/Search/Request/123) .

我们在网络服务器上实施了企业 SSO。 SSO 客户端拦截 http 请求,并在身份验证后使用 URL 重定向回请求的页面。

如果 # 片段未作为 http 请求的一部分发送 url,sso 无法重定向回同一页面。

我认为这很常见 scenario/issue。作为最后的手段,我会继续更改 URL 方案。例如(# 到 !)。

如何解决这个问题?

我会从你的申请中一起删除丑陋的 URL。

本文将引导您删除 asp.net-mvc 项目中难看的 URL。它还将确保您的 RouteConfig.cs 设置正确。

http://www.codeproject.com/Articles/806500/Getting-started-with-AngularJS-and-ASP-NET-MVC-P

分片(#后面URL的部分)不一定是浏览器发送到服务器端的。它们仅供客户端使用(导航到文档中的特定位置,JavaScript 支持)。

RFC 2396 第 4.1 节:

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

(强调)

因此,除非将 # 更改为另一个字符,否则您提出的 URL 方案将无法可靠地工作。或者,您可以 use JavaScript to transfer the information from the fragment in an input that will be reliably passed back to the server。但请注意,该解决方案仅在浏览器中启用 JavaScript 时才有效,因此它(也)不是适用于所有客户端的 100% 可靠解决方案。

无论哪种方式,使用不带片段的 URL 是一种更可靠的方法,如果您希望服务器解释该部分,IMO 是更好的设计选择。

刚找到一篇专门处理这个问题的博客:

http://codetunnel.io/how-to-persist-url-hash-fragments-across-a-login-redirect/

他提供了两个想法:

When the page loads there simply needs to be some JavaScript that accesses the hash fragment and appends it to the redirect URL in the hidden field. Here's an example using JQuery for simplicity

$(function () {
  var $redirect = $('[name="redirect"]');
  $redirect.val($redirect.val() + window.location.hash);
});

或者,或者,

Instead of appending the hash fragment to the hidden field value, you could avoid sending it to the server at all and simply append it to the form action URL.

$(function () {
  var $loginForm = $('#loginForm');
  var actionUrl = $loginForm.attr('action');
  $loginForm.attr('action', actionUrl + window.location.hash);
});