我在控制器文件夹中创建了一个名为Utils
的类。但是,无法找到和访问其静态方法。这是宣言:
using ProjectManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using Microsoft.AspNet.Identity;
using System.Web.Security;
namespace ProjectManager.Controllers
{
public static class Utils
{
public static bool isInRole(IPrincipal User, string roleName, ApplicationDbContext dbContext)
{
try
{
var currentUser = (from user in dbContext.Users
where user.Id == User.Identity.GetUserId()
select user).First();
RolePrincipal r = (RolePrincipal)User;
string[] rolesArray = r.GetRoles();
if (rolesArray.Contains(roleName))
return true;
}
catch (Exception ex)
{
return false;
}
return false;
}
}
}
答案 0 :(得分:1)
无论您想在何处使用VSB,都需要添加:
using ProjectManager.Controllers;
到班级的顶部,或者:
@using ProjectManager.Models
在你的Razor视图中。
编辑:正如@Martin Staufcik非常好地指出的那样,它可以添加到您的View文件夹web.config的命名空间部分,以便可以在该文件夹中的Views中访问,而无需在每个视图中使用@using。
但是,您仍然需要为每个后端类添加一个使用。
答案 1 :(得分:1)
如果在多个页面上使用该类,请将名称空间添加到Views文件夹中的web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="ProjectManager.Controllers" />
</namespaces>
</pages>
</system.web.webPages.razor>