C# 在.Net 中,是否可以在不继承自 System.Web.UI.Page 的类中使用 Response.Write

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/833830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:28:32  来源:igfitidea点击:

In .Net, is it possible to use Response.Write in a class that does not inherit from System.Web.UI.Page

c#.net

提问by Gais

Just been asked this question as a true / false in a telephone job interview and was a little stumped. Any ideas?

刚刚在电话面试中被问到这个问题是对是错,有点难住了。有任何想法吗?

采纳答案by Matthew Steeples

You can get hold of the Response object from the HttpContext static object:

您可以从 HttpContext 静态对象中获取 Response 对象:

System.Web.HttpContext.Current.Response.Write("Some Text")

If current returns null then the code is not being run as part of a page request. If you get asked the question again then that's the answer but a follow up question should be "why would you need to?"

如果 current 返回 null 则代码没有作为页面请求的一部分运行。如果你再次被问到这个问题,那就是答案,但后续问题应该是“你为什么需要?”

回答by TcKs

Yes, of course. If you have reference do instance of "Respone", you can call any method on them.

是的当然。如果您有“Respone”的引用 do 实例,您可以对它们调用任何方法。

回答by Mark Dickinson

The short answer is yes. In an interview id mention UserControl for an example.

简短的回答是肯定的。在一次采访中,id 以 UserControl 为例。