If we are referencing the same Feature or Permission Check in a razor cshtml or javascript, is there any advantage at all to storing the value locally so that it only has to do the lookup one? Or is this already handled some how with caching?
For example:
@{
/* Get permissions once */
var isGranted_Items_Modify = IsGranted(AppPermissions.Items_Modify);
var isGranted_Items_History = IsGranted(AppPermissions.Items_History);
/* Get features once */
var isEnabled_EntityHistory = FeatureChecker.IsEnabled(AppFeatures.EntityHistory);
}
@* Permissions required by Javascript *@
<input type="hidden" name="isGranted_Items_Modify" value="@isGranted_Items_Modify.ToString().ToLower()" />
And then using the these variables throughout the cshtml and javascript instead of calling IsGranted and IsEnabled each time?
We have some pages where we are doing a high number of calls to these methods and wondering if this is worth doing or not.
Thanks, Scott
2 Answer(s)
-
0
Hi @Astech
In this usage, it's appropriate to retrieve the values once and reuse them within Razor/JS. Especially if the same permission/feature result will be used multiple times within the same view, storing it in a variable improves readability and reduces unnecessary repetitive calls. The permission/feature system can utilize caching mechanisms on the application side; however, it's generally considered good practice not to recalculate the same value repeatedly within the view. For JavaScript, you can safely pass the boolean value calculated on the server to the page and use it in client-side UI behavior. Nevertheless, security decisions must always be protected by server-side authorization; client-side values should only be used for UI control.
Thank you
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
That's great, thank you for confirming.
Kind regards Scott
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)