2016-12-25 3 views
0
public class BaseController : Controller 
    { 
     // GET: Base 
     public static UserManager.User _CurrentUser; 
    } 
} 

このコードは、BaseConnectorの一部であり、_CurrrentUser.Idをoutputcacheのキーとして使用します。私はこれを行うことを試みたときにbaseControllerオブジェクトIDをoutputcacheのキーとして設定する方法

[OutputCache(Duration = 1200, VaryByCustom = _CurrentUser.Id)] 

、それは「属性に引数が一定exprssionでなければならない」と、また、静的に設定する必要がありますと言います。

私はこのプロパティを静的にすることができますが、それを定数式にすることで出力キャッシュに使用できます。

+0

表示アクションメソッド宣言コード等

[Authorize] public class BaseController : Controller { private UserModel _currentUser; public UserModel CurrentUser => _currentUser ?? (_currentUser = GetCurrentUser()); private UserModel GetCurrentUser() { UserModel currentUser; if (!User.Identity.IsAuthenticated) return null; try { var userDataFromCookie = CookieHelper.GetCookie(FormsAuthentication.FormsCookieName); if (string.IsNullOrWhiteSpace(userDataFromCookie)) throw new ArgumentException("Authentication cookie is null"); currentUser = JsonHelper.Deserialize<UserModel>(FormsAuthentication.Decrypt(userDataFromCookie)?.UserData); } catch (Exception) { throw; } return currentUser; } } 

クッキーヘルパー・メソッド – Rajput

答えて

1

AuthからCurrentUserIdを取得することをおすすめします。クッキー。私はそのように使う。その

public static string GetCookie(string key) 
    { 
     return HttpContext.Current.Request.Cookies[key] != null ? HttpContext.Current.Request.Cookies[key].Value : null; 
    } 
関連する問題