1

下面是 Asp.net mvc 代码,

        public void Index()
        {
            Response.Write("Hey");
            Response.Redirect("https://www.google.com");
        }

 OR

 public void Index()
        {
            Response.Redirect("https://www.google.com");
            Response.Write("Hey");
        }

在这里,重定向有效,但 Write() 无效。为什么优先考虑重定向?我的意思是为什么在 http 响应中使用 302 而不是 200。

注意:这不是为了解决任何实时场景。只是好奇地知道原因或潜在的行为。

4

3 回答 3

2

Respose.Write 正在工作,但是当您执行 Redirect 时,服务器会发送带有标头的响应:

HTTP/1.1 302 对象移动

服务器:Microsoft-IIS/5.0

位置:某处/newlocation.aspx

然后浏览器向某处/newlocation.aspx 发起另一个请求(假设它支持重定向),将其内容加载到浏览器中。

无论如何,如果响应流被缓冲(“嘿”),您将使用 Response.Redirect 覆盖此响应。

于 2013-04-09T06:50:21.360 回答
0

这里没有优先考虑 Redirect ,您在同一个函数中同时调用 Responce.Write 和 Responce.Redirect ,然后在写入页面后直接重定向到您给定的 url。

于 2013-04-09T06:50:33.320 回答
0

如果您有一些需要执行此类操作的真实场景,您可以在代码隐藏/控制器中执行 Response.Write(取决于您使用的是 WebForms 还是 MVC)并在HTML 页面的正文:

控制器:

public void Index()
{
    Response.Write("Hey");
}    

HTML:

<%
//response would show here
%>
<head>
    <meta http-equiv="refresh" content="10; url=https://www.google.com" />
</head>
于 2013-04-09T07:15:25.757 回答