3

is it possible to get HTTP status code from current request (HttpRequest)? I am redirecting one page to another page with code 301. But when I am on redirected page, the page has in its response status code 200. In a browser I can see right code (301).

2 Answers 2

10

When redirecting page to another (no matter if the status you redirect with 301 or 302 status), there are 2 requests:

  1. First request for which the response status is 301 (or 302) with the new location
  2. Second request for which the response status will be 200 (assuming everything is ok) with the content of the redirected page.

When you check Response.StatusCode on the redirecting page, it will be 301, but on the redirected page it will never be 301 (unless you're doing another redirection after the first one).

To get the HTTP status code from the response of current request, use:

HttpContext.Current.Response.Status // to get the string like '200 OK'
HttpContext.Current.Response.StatusCode // to get only the int value, e.g. 200
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I understand. Because when I do a redirect with custom status code, it is actual only for the current http context. So when I am on redirected page, I have new status code because I have new (independent) http context.
3

To simply see the status code:

HttpContext.Current.Response.StatusCode

1 Comment

So what do you want to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.