Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - How can I redirect to a URL?

How can I redirect the viewer to a URL?

I noticed that someone asked How to redirect to another webpage in JavaScript/jQuery?, but I'm not exactly sure where this should go.

I have tried in the controller with:
window.location.replace("http://192.168.1.109/MWT/Taglist/ShowMap" + LastId);
and in the view with:

<% if (BreakCount >= 8) {  
    var url = "http://192.168.1.109/MWT/Taglist/ShowMap" + LastId;  
    window.location.replace(url);  
} %>  

Neither of these work. In both places places window has a red squiggly line underneath it and when I hover over it the message says "The name 'window' does not exist in the current context."

Any help would be greatly appreciated!

=D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is tagged MVC 3, so I'll give you the answer for that in spite of the JavaScript example you listed. In your controller class use this code:

public ActionResult MyAction()
{
    // Use this for an action
    return RedirectToAction("ActionName");
    // Use this for a URL
    return Redirect("http://192.168.1.109/MWT/Taglist/ShowMap" + LastId);
}

This is occuring on the server, meaning that the client browser recieves a redirect response for which the browser will likely submit an additional request. If you return a page with JavaScript it will have to load a page, run the JavaScript (presuming it is enable on the client's browser), the load the next page. Among other problems, using JavaScript means that if the user presses the back button they will be repeatedly redirected again back to the page they are currently on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...