C# 从 HttpContext 获取当前 System.Web.UI.Page?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58123/
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
Get current System.Web.UI.Page from HttpContext?
提问by tsimon
This is actually a two part question. First,does the HttpContext.Current correspond to the current System.UI.Page object?
这实际上是一个两部分的问题。首先,HttpContext.Current 是否对应于当前的 System.UI.Page 对象?
And the second question, which is probably related to the first, is why can't I use the following to see if the current page implements an interface:
而第二个问题,可能与第一个问题有关,是为什么我不能使用以下内容来查看当前页面是否实现了接口:
private IWebBase FindWebBase()
{
if (HttpContext.Current as IWebBase != null)
{
return (IWebBase)HttpContext.Current.;
}
throw new NotImplementedException("Crawling for IWebBase not implemented yet");
}
The general context is that some controls need to know whether they are executing as a SharePoint webpart, or as part of an Asp.Net framework.
一般上下文是某些控件需要知道它们是作为 SharePoint Web 部件执行还是作为 Asp.Net 框架的一部分执行。
I have solved the problem by requiring the control to pass a reference to itself, and checking the Page property of the control, but I'm still curious why the above does not work.
我已经通过要求控件传递对自身的引用并检查控件的 Page 属性来解决该问题,但我仍然很好奇为什么上述方法不起作用。
The compiler error is: Cannot convert System.Web.HttpContext to ...IWebBase via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion or null type conversion.
编译器错误是:无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换将 System.Web.HttpContext 转换为 ...IWebBase。
采纳答案by Ash
No, from MSDN on HttpContext.Current: "Gets or sets the HttpContext object for the current HTTP request."
不,来自 MSDN 上的 HttpContext.Current:“获取或设置当前 HTTP 请求的 HttpContext 对象。”
In other words it is an HttpContext object, not a Page.
换句话说,它是一个 HttpContext 对象,而不是一个 Page。
You can get to the Page object via HttpContext using:
您可以使用以下方法通过 HttpContext 访问 Page 对象:
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
// Use page instance.
}
回答by Kilhoffer
You're looking for HttpContext.Handler. Since Page implements IHttpHandler, you'll obtain a reference to the currently executing page.You'll have to cast it, or at least try to cast it to the particular type you're looking for.
您正在寻找HttpContext.Handler. 由于 Page 实现了 IHttpHandler,您将获得对当前正在执行的页面的引用。您必须对其进行强制转换,或者至少尝试将其强制转换为您正在寻找的特定类型。
HttpContext.Currentsimply returns the singleton instance of HttpContext. Therefore, it is not and can never be, a page.
HttpContext.Current简单地返回 HttpContext 的单例实例。因此,它不是也永远不可能是一页。
回答by user452427
You may want to use HttpContext.Current.CurrentHandlerif you want the precise page that is currently executing. For example, a request for Default.aspx is sent, but an error is thrown and you do a Response.Transferto your custom ErrorHandler.aspx page. CurrentHandlerwill return the instance of ErrorHandler.aspx (if called after the error) whereas HttpContext.Current.Handlerwould return an instance of Default.aspx.
HttpContext.Current.CurrentHandler如果您想要当前正在执行的精确页面,您可能想要使用。例如,发送了对 Default.aspx 的请求,但抛出了错误,您Response.Transfer对自定义 ErrorHandler.aspx 页面执行了操作。CurrentHandler将返回 ErrorHandler.aspx 的实例(如果在错误之后调用),而HttpContext.Current.Handler将返回 Default.aspx 的实例。
回答by Amin Ghaderi
Please see my answer :
Why HttpContext.Current.Handler is null?
Maybe resolved your problem.
请看我的回答:
为什么 HttpContext.Current.Handler 为空?
也许解决了你的问题。

