0

I want to return BadRequest status code from Initialize method. I understand, how can I do it from any Action ( return new HttpStatusCodeResult(HttpStatusCode.BadRequest) ), but how to do it from Initialize? I try the following:

requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;

but I get

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

if I don't call

base.Initialize(requestContext);
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159

2 Answers2

0

thats by design, you should override Initialize method and call it on the base method in order for controller to get http context.

https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.initialize(v=vs.118).aspx

dee zg
  • 13,793
  • 10
  • 42
  • 82
0

You can use HttpContext.Current like;

HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadRequest; HttpContext.Current.Response.End(); return;
TSungur
  • 396
  • 2
  • 9
  • return an empty page – Oleg Sh Jun 30 '17 at 14:21
  • If you don't want an empty page just change the status. Another interesting point, if you want to process the page and not return empty, what reason you have to not call "base.Initialize"? – TSungur Jun 30 '17 at 14:43
  • I just want to return a page which returned when we call return new HttpStatusCodeResult(HttpStatusCode.BadRequest) – Oleg Sh Jun 30 '17 at 14:57