Error creating control - Response is not available in this context


I bet! You might have came across this error, at least once in your development, while playing with master pages.

Error creating control
Response is not available in this context

But still, the page will render during runtime, without any issues.

aspnet_error_creating_control_response_not_available_in_this_context


I came across this error last night. Though, I have been seeing this error for a last few years, I clearly don’t have an idea what’s exactly the reason for this design-break. But, now I know, Why it’s behaving so!

Here goes the reason. All my content pages are inheriting a base class. One of the functionality of base class is to check for authorized users in the OnInit event of the webpage.

reason_aspnet_error_creating_control_response_not_available_in_this_context_reason


Now, what was the reason for the error?

Read the error message carefully – “Response is not available in this context”. Say it again, “Response is ….”. Now, check the above code again.

Inside the if statement, I’ve a Response.Redirect. In designer mode, Response is simply null because I haven’t executed my webpage yet and does not have any value.

Workaround? Yes, simply include a null check!

solution_aspnet_error_creating_control_response_not_available_in_this_context_solution


Now, rebuilt your website and check the design-mode.

Viola! That’s all.

Please note, objects like Session will also cause this problem because their value won’t be available in design-mode (read more).

Hope this helped.
Thanks!