码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-11 11:03:54      阅读:204      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!