2

我試圖用用Exention方法的IIdentity推廣的IIdentity

這裏是我的類:

public static class MyIdentity 
{ 
    public static string FullName(this IIdentity identity) 
    { 
     return "John Doe"; 
    } 
} 

,我想用我的看法是這樣的:

@Context.User.Identity.FullName() 

,但我得到了以下錯誤:

'System.Security.Principal.IIdentity' does not contain a definition for 'FullName' and no extension method 'FullName' accepting a first argument of type 'System.Security.Principal.IIdentity' could be found

回答

7

請確保您已帶來了此擴展方法被定義爲範圍視圖中的命名空間:或者,如果你想使用它的許多意見,避免加重該命名空間中的每個查看您還可以

@using NameSpaceInWhichTheMyIdentityStaticClassIsDefined 
@User.Identity.FullName() 

將其添加到的~/views/web.config<namespaces>的部分(不要與~/web.config混淆):

<add namespace="NameSpaceInWhichTheMyIdentityStaticClassIsDefined" />