spcontext.current为ihttplandler为null
-
10-12-2019 - |
题
我正在尝试在SharePoint 2010中实现IHTTPHANDLER,并参考我正在使用这个: http://blogs.msdn.com/b/kaevans/archive/2010/08/04/deploying-an-asp-net-htttpandler-to-SharePoint -2010.aspx
但我得到spcontext.Current是“null”,无法检索上下文网站。
URL就像:
http://spsvr/sitecoll/subsite/_layouts/myfolder/myhandler.ashx?param1=paramvalue
. 解决方案
我正在回答我的问题。
在审查@simondoy评论后,“这很奇怪”并意识到我真的犯了一些错误,然后实现错误。
我犯了错误,我在升高的权限中运行,在我参考spcontext.current中。
错误代码
public void ProcessRequest(HttpContext context)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
// 'SPContext.Current' null reference error
using (var site = new SPSite(SPContext.Current.Site.ID))
{
using (var web = site.OpenWeb(SPContext.Current.Web.ID))
{
// codes goes here
}
}
});
}
.
固定代码
public void ProcessRequest(HttpContext context)
{
var curSite = SPContext.Current.Site;
var curWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (var site = new SPSite(curSite.ID)) {
using (var web = site.OpenWeb(curWeb.ID))
{
// code goes here
}
}
});
}
.