How can I redirect to a URL?

前端 未结 5 2044
名媛妹妹
名媛妹妹 2020-12-15 16:09

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

相关标签:
5条回答
  • 2020-12-15 16:50

    Inside your controller call return RedirectToAction().

    public ActionResult MyAction() {
    
        return RedirectToAction("Index", "Home");
    }
    

    or, it you use T4MVC (and you should ;-))

    public ActionResult MyAction() {
    
        return RedirectToAction(MVC.Home.Index());
    }
    

    Do not put the if statement in the view - that's not the MVC way. It is the responsibility of the controller to decide whether to redirect to a different view.

    0 讨论(0)
  • 2020-12-15 16:51

    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.

    0 讨论(0)
  • 2020-12-15 16:55

    Try like this:

    <script type="text/javascript">
        // Make sure the LastId variable is defined
        var LastId = '123';
        <% if (BreakCount >= 8) { %>
            var url = "http://192.168.1.109/MWT/Taglist/ShowMap" + LastId;
            window.location.replace(url);
        <% } %>
    </script>
    
    0 讨论(0)
  • 2020-12-15 17:03

    Try this:

     <script type="text/javascript">
       var id = '123';
       location.href = "http://192.168.1.109/MWT/Taglist/ShowMap/" + id;
     </script>
    
    0 讨论(0)
  • 2020-12-15 17:06

    Try like this :

    top.location.href = "/url";

    0 讨论(0)
提交回复
热议问题