Skip to content

asp .net 页面跳转

L edited this page Mar 10, 2020 · 4 revisions

ajax异步

通过ajax去请求数据,然后在js里面得到返回结果,赋值location.href

<div>
    <input id="url" />
    <button onclick="RedirectByAjax()">跳转页面</button>
</div>
<script>
    function RedirectByAjax() {
        //ajax请求完成一些工作
        $.ajax({
            type: "POST",
            url: "/RedirectUrl/RedirectByAjax",
            data: {
                url: $("#url").val()
            },
            success: function (url) {
                //得到结果,跳转页面
                location.href = url;
            }
        });
    }
</script>

form同步

通过post form表单提交数据,然后在表单对应的Action里面调用Redirect

<form id="redirectForm" name="redirectForm" action="/RedirectUrl/RedirectByForm" method="post">
    <div>
        <input id="url" name="url" />
        <button id="submit" onclick="Submit()">登录</button>
    </div>
</form>
<script>
    function Submit() {
        $("#redirectForm").submit();
    }
</script>
[HttpPost]
public ActionResult RedirectByForm(string url)
{
    return Redirect(url);
}

注意

二者不可混用,比如ajax请求+Redirect,这样返回的页面在Network请求里面,而不是实现跳转页面

其他

使用Response.Redirect 方法
使用Server.Transfer方法
使用Server.Execute 方法
使用javascript脚本(上面两种方式都属于这种)
使用超链接

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/Website/DotNetFramework/NotePractice/Views/RedirectUrl

Clone this wiki locally