我有以下 Asp.Net MVC 4 代码,它在 Controller DealController
的第 18 行引发错误。目前,只有当我在本地 PC 上使用 VS 2012 进行调试时重建项目后才会发生这种情况。
Line 16: private IQueryable<Deal> Deals
Line 17: {
Line 18: get { return User.IsInRole("Admin") ? _db.Deals : _db.Deals.Where(d => d.CreatedBy == User.Identity.Name); }
Line 19: }
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
我应该在所有调用 User.IsInRole()
或 User.Identity.Name
的 Controller 上添加属性 [InitializeSimpleMembership]
吗?目前它仅添加到 AccountController
上。
最佳答案
为 Controller 添加[InitializeSimpleMembership]
解决了该问题。
关于asp.net - 之前必须调用 "WebSecurity.InitializeDatabaseConnection"方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15457597/