Best Pattern for AllowUnsafeUpdates

前端 未结 6 1584
半阙折子戏
半阙折子戏 2020-12-28 08:54

So far, in my research I have seen that it is unwise to set AllowUnsafeUpdates on GET request operation to avoid cross site scripting. But, if it is required to allow this,

6条回答
  •  孤城傲影
    2020-12-28 09:00

    Another clean way to implement would be to use a combination of extension methods and anonymous delegates as such:

    public static void DoUnsafeUpdate(this SPWeb web, Action action)
    {
        bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
        web.AllowUnsafeUpdates = true;
        action();
        web.AllowUnsafeUpdates = allowUnsafeUpdates;
    }
    

    Using the above extension method, you can then perform your "unsafe update" action as follows:

    var web = SPContext.Current.Web;
    web.DoUnsafeUpdate(delegate()
    {
        // Put your "unsafe update" code here
    });
    

提交回复
热议问题