我有一个MVC模板,我有2个用户角色,只有具有这些角色的用户才能看到并单击导航栏中的listItem。但由于所有角色都可以看到它,所以只是不向未登录的人显示它就足够了。这是我的cshtml
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Roles", "Index", "Roles")</li>
<li>@Html.ActionLink("Evaluaties", "About", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
因此,最后一个listItem评估不应该通过未登录人员或其他单词只能由Student(Leerling)和Teacher(Begeleider)这两个角色来查看。我的控制器中使用了哪些。
public ActionResult About()
{
if (User.IsInRole("Begeleider"))
{
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
var jsonEvaluaties = client.DownloadString(new Uri("http://localhost:8080/projecten/api/evaluaties"));
var evaluaties = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Evaluatie>>(jsonEvaluaties);
ViewBag.Message = leerlingen;
ViewBag.Evaluaties = evaluaties;
}
if (User.IsInRole("Leerling"))
{
var email = User.Identity.GetUserName();
var client = new WebClient();
var jsonLeerlingen = client.DownloadString(new Uri("http://localhost:8080/projecten/api/leerlingen"));
var leerlingen = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Leerling>>(jsonLeerlingen);
var jsonEvaluaties = client.DownloadString(new Uri("http://localhost:8080/projecten/api/evaluaties"));
var evaluaties = Newtonsoft.Json.JsonConvert.DeserializeObject<IEnumerable<Evaluatie>>(jsonEvaluaties);
ViewBag.Message = leerlingen;
ViewBag.Evaluaties = evaluaties;
}
return View();
}
我尝试使用if (User.IsInRole("Begeleider"))
,但我无法在a.cshtml页面中使用它
答案 0 :(得分:3)
如果您查看@if (Request.IsAuthenticated)
{
<div>You are logged in</div>
}
else
{
<div>You are not logged in</div>
}
局部视图,您会看到实现的方式与尝试完全相同。例如:
{{1}}