MVC 5 IsInRole在剃刀视图上的用法:无法连接到数据库 [英] MVC 5 IsInRole Usage on Razor Views: Cannot connect to Database

查看:37
本文介绍了MVC 5 IsInRole在剃刀视图上的用法:无法连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MVC 5中的新身份系统时遇到问题,我的目标是在Views上使用User.IsinRole("RoleName").例如:

I'm having issues using the new identity system in MVC 5, my goal is to make use of the User.IsinRole("RoleName") on Views. For example:

@if(User.IsInRole("Administrator"))
 {
   <li>@Html.ActionLink("Admin", "Index", "Admin")</li>
 }

这放置在主布局页面中,在应用程序启动时会被点击.这样做时,出现以下错误:

This is placed in the main layout page which is hit when the application launches. On doing this, i'm getting the following error:

"System.Web.dll中发生类型'System.Web.HttpException'的异常,但未在用户代码中处理

"An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

其他信息:无法连接到SQL Server数据库."

Additional information: Unable to connect to SQL Server database."

我在上下搜索了一个解决方案,常见的解决方案是在控制器顶部包括"[InitializeSimpleMembership]",或者在应用程序启动时手动初始化数据库连接.(使用WebSecurity.InitializeDatabaseConnection).这两种方法似乎都未被MVC 5识别.

I have searched high and low for a solution to this, the common solution is to either include "[InitializeSimpleMembership]" at the top of the controller or initialise the database connection manually in application start. (With WebSecurity.InitializeDatabaseConnection). Both of these methods do not seem to be recognised by MVC 5.

我还尝试通过每次使用Aspnet.Identity.UserManager确定角色来返回一个用IsAdmin布尔值填充ViewBag的视图时创建一堆凌乱的代码来解决此问题.虽然这行得通,但这并不是我觉得应该做的事情.

I have also tried working around this by creating a bunch of messy code any time i return a view to populate the ViewBag with an IsAdmin boolean by using the Aspnet.Identity.UserManager to determine roles. Whilst this works it's not the way i feel i should be doing things.

可能值得注意,但是我在后端访问User.IsInRole时没有遇到这些问题,这肯定是初始化问题.

It might be worth noting but i don't experience these issues accessing User.IsInRole on the backend, this definitely seems to be an initialization problem.

推荐答案

我遇到了同样的问题,但是Stunt的答案对我不起作用.尝试了

I was having the same problem, however Stunt's answer wasn't working for me. Tried the answer to this question and that solved the issue.

对于懒惰的您可以尝试将其添加到您的web.config文件中:

For the lazy you can try adding this to your web.config file:

 <system.webServer>
    <modules>
      <remove name="RoleManager" />
    </modules>
  </system.webServer>

这篇关于MVC 5 IsInRole在剃刀视图上的用法:无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆