【问题标题】:Meet with error -- "Updates are currently disallowed on GET requests"遇到错误——“当前不允许对 GET 请求进行更新”
【发布时间】:2011-01-20 01:25:03
【问题描述】:

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

这是我的代码 sn-ps,我收到错误 - “当前不允许对 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);
            }
        }

【问题讨论】:

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


    【解决方案1】:

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

    不管怎样,如果你愿意,你可以把这个 SPWeb.AllowUnsafeUpdates 设置为 true,但是像这样使用它:

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

    【讨论】:

    • 你的意思是如果我把你的代码放到 aspx 的 Page_Load 中的 try/catch 块中?
    • 是的,您将此代码包装在您调用 SPSiteCollection 对象的 .Add 方法的位置。并且不要循环设置 AllowUnsafeUpdates 为 true 的 SPWeb 对象(甚至不调用 SPWeb.Dispose)。请参阅此处:msdn.microsoft.com/en-us/library/aa973248.aspx SPSite 本身具有 AllowUnsafeUpdates 属性 - 您可能应该使用它。
    【解决方案2】:

    在您尝试允许更新之前,添加一项检查以确保您收到的是 POST 而不是 GET。确保所做的任何更改都是通过 POST 请求而不是使用 URL 参数和 GET 进行的。

    if (IsPostBack)
    {
       ...
    }
    

    【讨论】:

    • 我想把所有的东西都包装在try 块中,尽管如果请求作为get 发送,这仅仅意味着它什么都不做——没有例外,但也没有执行任何代码。
    • 为什么get方法不能用?我认为从错误消息中,这意味着当我们启用 AllowUnsafeUpdates 时可以使用 get。有没有cmets?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2015-03-09
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多