Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript重定向_Javascript_Asp.net_.net_Asp.net Mvc 3_Dom - Fatal编程技术网

使用javascript重定向

使用javascript重定向,javascript,asp.net,.net,asp.net-mvc-3,dom,Javascript,Asp.net,.net,Asp.net Mvc 3,Dom,我有一个ASP.NET MVC 5应用程序,我想在其中使用Javascript执行重定向: var sPageURL = decodeURIComponent(window.location); console.log(sPageURL); var lengthUrl = sPageURL.split('/').length; var NewUrl = ''; sPageURL.split('/').forEach(function(item, index) { if (index <

我有一个ASP.NET MVC 5应用程序,我想在其中使用Javascript执行重定向:

var sPageURL = decodeURIComponent(window.location);
console.log(sPageURL);
var lengthUrl = sPageURL.split('/').length;
var NewUrl = '';
sPageURL.split('/').forEach(function(item, index) {
  if (index < lengthUrl - 2) NewUrl += item;
  if (index == lengthUrl - 1) {
    if (item == "Organisateur") NewUrl += sPageURL.split('/')[lengthUrl - 2];
  }
});
console.log(NewUrl);
window.location = NewUrl;
var sPageURL=decodeURIComponent(window.location);
日志(sPageURL);
var lengthUrl=sPageURL.split('/').length;
var NewUrl='';
sPageURL.split('/').forEach(函数(项,索引){
如果(索引
我得到的结果是:

问题是新URL与旧URL连接在一起:我想
http://localhost:31569/Event/2
替换为
localhost:31569

我尝试了
window.location=
window.location.href=
window.location.replace
,得到了相同的结果

所以我需要知道:

  • 这个问题的原因是什么
  • 我怎样才能修好它

  • 使用
    window.location=“/”
    应该导航到网页的主机名,我认为这正是您要做的


    您遇到的问题听起来像是您没有在要导航到的URL前面包含协议(
    http://
    https://
    )。

    如果您想导航到URL的根目录,为什么不执行
    window.location=“/”
    ?@KianCross good:一个简单而好的解决方案。但我仍然不明白我的代码有什么问题(请将您的评论作为答案发布)