Why do I get “Cannot redirect after HTTP headers h

2019-01-03 02:21发布

When I call Response.Redirect(someUrl) I get an HttpException: "Cannot redirect after HTTP headers have been sent".

Why do I get this? And how can I fix this issue?

15条回答
Bombasti
2楼-- · 2019-01-03 03:09

You can also use below mentioned code

Response.Write("<script type='text/javascript'>"); Response.Write("window.location = '" + redirect url + "'</script>");Response.Flush();
查看更多
该账号已被封号
3楼-- · 2019-01-03 03:12

Error Cannot redirect after HTTP headers have been sent.

System.Web.HttpException (0x80004005): Cannot redirect after HTTP headers have been sent.

Suggestion

If we use asp.net mvc and working on same controller and redirect to different Action then you do not need to write..
Response.Redirect("ActionName","ControllerName");
its better to use only
return RedirectToAction("ActionName");
or
return View("ViewName");

查看更多
Deceive 欺骗
4楼-- · 2019-01-03 03:13

Once you send any content at all to the client, the HTTP headers have already been sent. A Response.Redirect() call works by sending special information in the headers that make the browser ask for a different URL.

Since the headers were already sent, asp.net can't do what you want (modify the headers)

You can get around this by a) either doing the Redirect before you do anything else, or b) try using Response.Buffer = true before you do anything else, to make sure that no output is sent to the client until the whole page is done executing.

查看更多
登录 后发表回答