URL中的哈希(#)符号
本文关键字:符号 哈希 URL | 更新日期: 2023-09-27 18:01:36
我想创建一个这样的URL
www.site.com/?q=house
但是当我打开网站时,我得到
www.site.com/#
javascript后的
window.location.hash = "q=house";
URL看起来像
www.site.com/#q=house
哈希从何而来?如何从url中删除它?
我的路由配置,如果它是重要的
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
使用MVC 5
您正在设置哈希而不是查询字符串。这就是为什么。
http://www.w3schools.com/jsref/prop_loc_hash.asp你应该像这样使用window.location.href
:
window.location.href = window.location.href + "?q=house";
如果你想更新一个参数,你可以使用以下方法:
function UpdateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi");
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
return url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
var hash = url.split('#');
url = hash[0].replace(re, '$1$3').replace(/(&|'?)$/, '');
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
url += '#' + hash[1];
return url;
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?',
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (typeof hash[1] !== 'undefined' && hash[1] !== null)
url += '#' + hash[1];
return url;
}
else
return url;
}
}
来源:添加或更新查询字符串参数