Lukas Dvorak
Lukas Dvorak

Reputation: 481

Get status code from HttpRequest

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).

Upvotes: 3

Views: 5816

Answers (2)

Marek Musielak
Marek Musielak

Reputation: 27142

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

Upvotes: 10

TheCatWhisperer
TheCatWhisperer

Reputation: 981

To simply see the status code:

HttpContext.Current.Response.StatusCode

Upvotes: 3

Related Questions