2010-02-23 64 views
0

我正在将SharePoint Server 2007 Enterprise与Windows Server 2008 Enterprise配合使用。我部署了一个发布门户。我正在开发使用VSTS 2008 + C#+ .Net 3.5 + ASP.Net + SharePoint Server 2007 SDK的ASP.Net Web应用程序。遇到错误 - “GET请求当前不允许更新”

这里是我的代码片断,我得到错误 - “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", "[email protected]"); 
      } 
      catch (Exception ex) 
      { 
       Response.Write(ex.ToString() + "\t" + ex.StackTrace); 
      } 
     } 

回答

1

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

无论如何,如果你喜欢,你可以把这个SPWeb.AllowUnsafeUpdates为真,但像这样使用:

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

你的意思是,如果我把你的代码放入aspx的Page_Load中的try/catch块中? – George2 2010-02-24 13:39:13

+0

是的,您可以将此代码包装在您调用SPSiteCollection对象的.Add方法的位置。 并且不要循环SPWeb对象将AllowUnsafeUpdates设置为true(并且您甚至不会调用SPWeb.Dispose)。看到这里:http://msdn.microsoft.com/en-us/library/aa973248.aspx SPSite iteself有AllowUnsafeUpdates属性 - 你应该使用它。 – 2010-02-24 14:28:22

+0

谢谢,问题回答! – George2 2010-02-25 05:07:07

1

添加检查以确保您在尝试允许更新之前获取POST而不是GET。确保无论是通过POST请求进行更改,而是使用URL参数和GET。

if (IsPostBack) 
{ 
    ... 
} 
+0

如果检查到收藏此? – George2 2010-02-23 13:07:47

+1

将所有内容都包裹在'try'块中,我会想象,尽管如果请求是作为get来发送的,这只会意味着它什么都不做 - 没有例外,但是也没有执行任何代码。 – tvanfosson 2010-02-23 13:12:18

+0

为什么不能使用方法?我从错误信息中想到,这意味着当我们启用AllowUnsafeUpdates时可以使用get。任何意见? – George2 2010-02-23 13:18:16

相关问题