This method is a constructor that has to decide which URL to add to the response header as Allow-Origin. i am actually adding the Allow-Origin later according to the value of PermittedOrigion variable. I am using Session to eliminate database access.
Could i write it more efficient? I am working with asp.net webform
public ResponseAsJson(HttpRequest request, int affiliateId)
{
string cacheKeyName = $"PermittedOrigion_{affiliateId.ToString()}";
const string notDefined = "N";
object ValueFromCache = HttpContext.Current.Cache[cacheKeyName];
if (!String.IsNullOrWhiteSpace(ValueFromCache?.ToString()))
{
if (ValueFromCache.ToString() == notDefined)
return;
PermittedOrigion = ValueFromCache.ToString();
return;
}
string actualOrigion = null;
if (request.UrlReferrer != null)
actualOrigion = request.UrlReferrer.GetLeftPart(UriPartial.Authority);
string[] strFromDB = RM.BL.Affiliate.Instance(affiliateId).LUAffiliate.AccessControlAllowOriginJsonResult.Split(';');
if (!String.IsNullOrWhiteSpace(actualOrigion) && strFromDB.Contains(actualOrigion))
{
PermittedOrigion = actualOrigion;
}
else
{
string strLocalhost = strFromDB.Where(x => x.ToLower().StartsWith("http://localhost")).FirstOrDefault();
if (!String.IsNullOrWhiteSpace(strLocalhost))
PermittedOrigion = strLocalhost;
}
if (PermittedOrigion == null)
{
HttpContext.Current.Cache[cacheKeyName] = notDefined;
}
else
{
HttpContext.Current.Cache[cacheKeyName] = PermittedOrigion;
}
}