我在使用现有网址进行重定向时遇到了麻烦。我有一个带有Google图表的页面,在该页面上,我向条形图添加了一个侦听器。当用户单击条形图之一时,一条case语句将确会被递给路由器的ID。
google.visualization.events.addListener(wrapper, 'select', selectHandler);
function selectHandler() {
var selection = wrapper.getChart().getSelection()
var monthsTransform = 0;
switch(selection[0].column)
{
case 1:
monthsTransform = 0;
break;
case 2:
monthsTransform = 3;
break;
case 3:
monthsTransform = 6;
break;
case 4:
monthsTransform = 12;
break;
case 5:
monthsTransform = 18;
break;
//no need to default as already set to 0!
}
var url = "newController/newPage/" + monthsTransform;
window.location.href = url;
}
我的问题是:
window.location.href
将newController/newPage附加到现有的url,最终结果如下:
localhost:1234/oldController/oldPage/newController/newPage/id
Use window.location.assign("/newController/newPage/" + monthsTransform), instead of window.location.href
It would even be nicer if you added the server name and protocol in front of the new URL, and used Html.Action html helper to generate the url.
Check my blog post on creating URL's on the server side:
http://ilovedevelopment.blogspot.co.uk/2012/04/aspnet-mvc-3-generating-actionlink-in.html
The crux of it I've pasted below for brevity - modify as required:
string absoluteAction = string.Format("{0}://{1}{2}",
Url.RequestContext.HttpContext.Request.Url.Scheme,
Url.RequestContext.HttpContext.Request.Url.Authority,
Url.Action("ActionName", "ControllerName", new {Id = "Data"}));
This can be done inside a controller method so you could make an ajax call and return the proper url "absoluteAction" as a Json result.