c# - ASP MVC Make Class Namespace Available In All Views -


i have following permission class. how can make available in views without having use following using statement in each view @using myapp.extensions

using system.security.principal;  namespace myapp.entensions {      public class permissions      {          private readonly iprincipal user;           public permissions(iprincipal user)          {              this.user = user;          }            public bool canedititems          {             { return user.isinanyrole("manager", "admin"); }          }          public bool candeleteitems         {            { return user.isinanyrole("admin"); }         }          // other permissions       }  } 

i've tried adding views web.config follows don't intellisense when tryin call @if (user.candeleteitems)

<pages pagebasetype="system.web.mvc.webviewpage">   <namespaces>     <add namespace="system.web.mvc" />     <add namespace="system.web.mvc.ajax" />     <add namespace="system.web.mvc.html" />     <add namespace="system.web.optimization"/>     <add namespace="system.web.routing" />     <add namespace="myapp.extensions" />  </namespaces> </pages> 

if want add extensions iprinciple need extension class, so

using system.security.principal;  namespace myapp.entensions {      public static class permissions      {          public static bool canedititems(this iprinciple user)          {             return user.isinanyrole("manager", "admin");           }          public static bool candeleteitems(this iprinciple user)         {            return user.isinanyrole("admin");          }          // other permissions       }  } 

not class public + static, , extension methods public + static , have "this" before first parameter. need change view use canedititems() method instead of property.

if (system.web.httpcontext.current.user.canedititems()) { ... } 

Comments