遇到错误 - “目前GET请求中不允许更新”

时间:2010-02-23 12:37:06

标签: c# asp.net .net visual-studio-2008 sharepoint-2007

我在Windows Server 2008 Enterprise中使用SharePoint Server 2007 Enterprise。我已经部署了一个发布门户。我正在使用VSTS 2008 + C#+ .Net 3.5 + ASP.Net + SharePoint Server 2007 SDK开发ASP.Net Web应用程序。

这是我的代码片段,我收到错误 - “目前GET请求中不允许更新”。任何想法如何解决?

Microsoft.SharePoint.SPException: Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteCollections = webApp.Sites;
                foreach (SPSite item in siteCollections)
                {
                    SPWeb webItem = item.OpenWeb();
                    webItem.AllowUnsafeUpdates = true;
                }

                SPSite newSiteCollection = siteCollections.Add("sites/myblog",
                    "New Sample Site", "New Sample Site Description", 1033, "BLOG#0",
                    "foo\\foouser", "Owner name", "owneremail@foo.com");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString() + "\t" + ex.StackTrace);
            }
        }

2 个答案:

答案 0 :(得分:1)

添加检查以确保在尝试允许更新之前获得POST而不是GET。确保进行更改的任何内容都通过POST请求而不是使用URL参数和GET进行。

if (IsPostBack)
{
   ...
}

答案 1 :(得分:1)

为什么不允许在GET请求上读/写数据库的问题是因为您的代码可以通过跨站点脚本来利用。 Read about AllowUnsafeUpdates consequences here

无论如何,如果你愿意,你可以将这个SPWeb.AllowUnsafeUpdates设置为true,但是像这样使用它:

try {
  web.AllowUnsafeUpdates = true;
  ...
}
finally {
  web.AllowUnsafeUpdates = false;
}