当“SPSite”打开时,Web 部件正在本地服务器上运行,但无法在生产环境中运行。类在 web 部件中使用

发布于 2024-09-10 14:59:21 字数 1980 浏览 13 评论 0原文

当 Web 部件中使用“SPSite”类时,Web 部件可以在本地服务器上运行,但不能在生产服务器上运行。在生产服务器上它会抛出错误。如果我不在 Web 部件中使用该类,则 Web 部件也可以在实时计算机上运行。

知道什么可能导致错误吗?

webpart 中的代码是这样的:

namespace CompanyNews
{
    [Guid("40de3c60-9e30-4050-b9f3-01e71868f522")]
    public class CompanyNews : System.Web.UI.WebControls.WebParts.WebPart
    {
        private HtmlTextWriter writer;
        public CompanyNews()
        {
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string listName = "News Display";
                    writer.Write(listName);
                    SPList list = null;
                    foreach (SPList currentList in web.Lists)
                    {
                        if (currentList.Title.Equals(listName,
                                                     StringComparison.InvariantCultureIgnoreCase))
                        {
                            list = currentList;
                            break;
                        }
                    }
                    writer.WriteBeginTag("ul");
                    foreach (SPListItem item in list.Items)
                    {                    
                        writer.Write("<li style=\"font-size:12px;padding:1px\">");
                        writer.Write(item["Title"].ToString() + "...   ");
                        writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>");
                        writer.Write("</li>");
                    }
                    writer.WriteEndTag("ul");
                }
            }
        }

    }
}

webpart 的 dll 位于 bin 文件夹中,并且在 web.config 文件中有一个 web par 条目作为安全控件。 其他显示“helloworld”消息的 Web 部件也以相同的方式上传到生产环境。 我猜是导致问题的代码。

错误消息为:“预览 Web 部件时发生错误”

webpart is working on local server but not working on production server when "SPSite" class is uesd in the web part. On the production server it throws error. If I do not use that class in the webpart the web part also works on live machine.

Any idea what might be causing the error?

the code in the webpart is this:

namespace CompanyNews
{
    [Guid("40de3c60-9e30-4050-b9f3-01e71868f522")]
    public class CompanyNews : System.Web.UI.WebControls.WebParts.WebPart
    {
        private HtmlTextWriter writer;
        public CompanyNews()
        {
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string listName = "News Display";
                    writer.Write(listName);
                    SPList list = null;
                    foreach (SPList currentList in web.Lists)
                    {
                        if (currentList.Title.Equals(listName,
                                                     StringComparison.InvariantCultureIgnoreCase))
                        {
                            list = currentList;
                            break;
                        }
                    }
                    writer.WriteBeginTag("ul");
                    foreach (SPListItem item in list.Items)
                    {                    
                        writer.Write("<li style=\"font-size:12px;padding:1px\">");
                        writer.Write(item["Title"].ToString() + "...   ");
                        writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>");
                        writer.Write("</li>");
                    }
                    writer.WriteEndTag("ul");
                }
            }
        }

    }
}

The dll of the webpart is in the bin folder and in the web.config file there is an entry for the web par as a safe control.
Other webpart which displays a "hellow world" message is also uploaded to production the same way.
I i guess its the code that is causing the problem.

The error message is: "An error occurred while previewing the web part"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-09-17 14:59:21

只是我注意到的一点是,您不应该将当前上下文中的对象包装在 using 语句中。这里有好文章 Clicky

更好的做法是使用以下内容

using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
{
...
} 

此外,您还应该考虑将您的解决方案打包在 WSP 中,以允许 stsadm 部署它。拖入 GAC 并不是一个很好的做法。

谢恩

just something I noticed, you shouldn't wrap objects from the Current Context in a using statement. Good article here Clicky

Better practice would be to use the following

using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
{
...
} 

Also you should look at packaging up your solution in a WSP, allowing stsadm to deploy it. Dragging into the GAC isn't very good practice.

Shane

忆悲凉 2024-09-17 14:59:21

SPSite 对象在我能看到的任何地方都没有得到引用。为什么不删除它,因为它对您的需求来说是多余的?

SPWeb web = SPContext.Current.Web;  
                string listName = "News Display"; 
                writer.Write(listName); 
                SPList list = null; 
                foreach (SPList currentList in web.Lists) 
                { 
                    if (currentList.Title.Equals(listName, 
                                                 StringComparison.InvariantCultureIgnoreCase)) 
                    { 
                        list = currentList; 
                        break; 
                    } 
                } 
                writer.WriteBeginTag("ul"); 
                foreach (SPListItem item in list.Items) 
                {                     
                    writer.Write("<li style=\"font-size:12px;padding:1px\">"); 
                    writer.Write(item["Title"].ToString() + "...   "); 
                    writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>"); 
                    writer.Write("</li>"); 
                } 
                writer.WriteEndTag("ul"); 

The SPSite object isn't getting reference anywhere that I can see. Why don't you remove it anyway as it's superflous to your needs?

SPWeb web = SPContext.Current.Web;  
                string listName = "News Display"; 
                writer.Write(listName); 
                SPList list = null; 
                foreach (SPList currentList in web.Lists) 
                { 
                    if (currentList.Title.Equals(listName, 
                                                 StringComparison.InvariantCultureIgnoreCase)) 
                    { 
                        list = currentList; 
                        break; 
                    } 
                } 
                writer.WriteBeginTag("ul"); 
                foreach (SPListItem item in list.Items) 
                {                     
                    writer.Write("<li style=\"font-size:12px;padding:1px\">"); 
                    writer.Write(item["Title"].ToString() + "...   "); 
                    writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>"); 
                    writer.Write("</li>"); 
                } 
                writer.WriteEndTag("ul"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文