Setting response status code manually

2019-02-19 10:07发布

I have a try catch block to handle an error I am getting with my application. I would like a simple way of setting the response to status code 403 or forbidden and then either redirect the user to the login page or to a custom error page.

I am having some issue with once setting the status code and the redirect. Anyone have an example of setting the status code and then redirecting?

标签: http redirect
2条回答
看我几分像从前
2楼-- · 2019-02-19 10:51

Duplicate: What should the HTTP response be when the resource is forbidden but there’s an alternate resource?

Sending Location header is only intended for 3xx (redirect) or 201 Created responses. Although it may work with most of the clients it's IMHO not how HTTP was designed.

If you really care about correct status codes and following HTTP specification why don't you respond with 303 See Other or use HTTP authentication.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-19 10:55
Response.Status = "403 Forbidden";
Response.Addheader("Location", "http://stackoverflow.com/");

This in C#, but the concept should be pretty much the same in most languages.

查看更多
登录 后发表回答