我有一个自定义 jQuery 控件,它使用 History.js 通过 Ajax 更改browserstate
和加载子面板。在 HTML 5 浏览器中,URl 已完全更改,因此页面刷新工作(加载当前面板)。
但是在 IE9 和更早版本中,状态存储为哈希值,例如:
http://localhost:63559/ApplicationPanel/ListView#/SectionPanel/ListView/3?&_suid=1397574319099025255064889015816
我想提取其中的哈希 URL(例如/SectionPanel/ListView/3
)OnActionExecuting
,以便我可以在页面刷新时重定向到正确的起点(后退和前进按钮可以正常工作)。
基本上我希望它这样做,但即使raw URL
不包含哈希部分。
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var url = filterContext.HttpContext.Request.RawUrl;
if (url.Contains('#'))
{
url = url.Substring(url.IndexOf('#')+1);
filterContext.Result = Redirect(url);
}
base.OnActionExecuting(filterContext);
}
如果我断点此代码,则 url 仅包含基本 URL(例如/ApplicationPanel/ListView
)。
如何获取 MVC 控制器中的哈希参数?我想我可能还需要更改它们的推送方式,但以下几种变体没有帮助:
// If HTML4, try adding a querystring
if (typeof window.history.pushState !== "function")
{
url = "?" + url;
}
this.Historyjs.pushState({ somedata: data }, "", url);
我得到一个像这样的网址:
http://localhost:63559/Level1#Level1/?/Level2/Index/3&_suid=139757562428301418756129738284
更新:
看来这取决于 History.js 操作浏览器 url 的方式。在 HTML 4 浏览器上,它只能修改哈希标记,因此服务器永远不会看到额外的数据。
作为测试,我让插件检查哈希 URL 并将浏览器重定向到它:
// If HTML4, extract the URL from the hash
if (typeof window.history.pushState !== "function")
{
var hash = window.location.hash;
if (hash.length)
{
var q = hash.indexOf('?');
if (q < 1)
{
q = (hash.length);
}
hash = hash.substring(1, q);
window.location = hash;
}
}
虽然这适用于页面刷新,但不幸的是,浏览器页面加载无法区分页面重新加载和按下后退按钮。此代码一遍又一遍地将后退按钮发送到同一个子页面。
有什么建议么?