Вопрос

Do I need to reset the SPWeb.AllowUnsafeUpdates property after use?

Seems not, but all over the web there is code like:

bool allowUnsafeOrig = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;

// ....

web.AllowUnsafeUpdates = allowUnsafeOrig;

So, is it needed or not?

Это было полезно?

Решение

Ideally, you should neither set or reset the value of AllowUnsafeUpdates. Instead use the SPUtility.ValidateFormDigest and that's it. This sets AllowUnsafeUpdates = true for the current HTTP request.

The reason you should never set the AllowUnsafeUpdates = false is that other code might use SPUtility method an rely on that it was earlier asserted. Than your code runs in the meantime and sets the value to false which breaks the code.

Другие советы

"If you are going to use AllowUnsafeUpdates, there's no need to reset it to its previous value. It does not get persisted. It's just something you need to set on an SPWeb object before performing updates from a GET (or other cases)"

from this question: https://stackoverflow.com/questions/213882/best-pattern-for-allowunsafeupdates

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top