首页 > 其他 > 详细

sharepoint 2010项目中,ashx页面获取SPContext.Current 为null的原因和解决方法

时间:2016-05-11 11:03:43      阅读:256      评论:0      收藏:0      [点我收藏+]
//错误的写法
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
            }
        }
    });

}

 

sharepoint 2010项目中,ashx页面获取SPContext.Current 为null的原因和解决方法

原文:http://www.cnblogs.com/lishidefengchen/p/5480990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!