Don't web.Dispose() dispose parent SPSite object?

Is it Ok if I do this in web part?

{              
    SPSite site = SPContext.Current.Site;

    using (SPWeb web = site.OpenWeb(s))
    {
         // ..
    }
}
有帮助吗?

解决方案

The code you written is Ok.

using statement will dispose the SPWeb object you have instantiated.

Parent Site - This object is not initiantiated by you, you are using SPSite that belongs to SPContext.

SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.

其他提示

Yes, it's acceptable to do what you have done. It's also ok to pass that web object to other functions.

See a detailed article on this subject from Microsoft here.

许可以下: CC-BY-SA归因
scroll top