2013-04-11 70 views
2

我第一次嘗試南希(真的很喜歡它),並遇到了一些問題。當我打電話Html.RenderContext.Context.CurrentUser我得到以下錯誤:Nancys Html.RenderContext.Context.CurrentUser thowing exception

Errors: 
[CS0012] Line: 5 Column: 6 - The type 'Nancy.ViewEngines.IRenderContext' is defined in an  assembly that is not referenced. You must add a reference to assembly 'Nancy, Version=0.16.2.0, Culture=neutral, PublicKeyToken=null'. (show) 
[CS1061] Line: 5 Column: 29 - 'Nancy.ViewEngines.IRenderContext' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'Nancy.ViewEngines.IRenderContext' could be found (are you missing a using directive or an assembly reference?) (show) 
[CS1061] Line: 9 Column: 63 - 'Nancy.ViewEngines.IRenderContext' does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type 'Nancy.ViewEngines.IRenderContext' could be found (are you missing a using directive or an assembly reference?) (show) 

的觀點是部分看起來像這樣:

<li class="divider-vertical"></li> 

@if (Html.RenderContext.Context.CurrentUser != null) 
{ 
    <div class="btn-group pull-right"> 
     <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> 
      <i class="icon-user"></i> @Html.RenderContext.Context.CurrentUser.UserName 
      <span class="caret"></span> 
     </a> 
     <ul class="dropdown-menu"> 
      <li><a href="/Dashboard">Content Dashboard</a></li> 
      <li><a href="/Login/SignOut">Log off</a></li> 
     </ul> 
    </div> 
} 
else 
{ 
    <li><a href="/Login">Login</a></li> 
} 

在管理器中啓用FormsAuthentication

FormsAuthentication.Enable(pipelines, new FormsAuthenticationConfiguration 
     { 
      RedirectUrl = "~/login", 
      UserMapper = container.Resolve<IUserMapper>() 
     }); 
    } 

Usermapper和身份的實現看起來像這樣

public class GoonUserMapper : IUserMapper 
{ 
    public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context) 
    { 
     return new GoonIdentity { UserName = "Testing" }; 
    } 
} 


public class GoonIdentity : IUserIdentity 
{ 
    public GoonIdentity() 
    { 
     this.Claims = new List<string>(); 
    } 

    public string UserName { get; set; } 

    public IEnumerable<string> Claims { get; set; } 
} 

我使用下列程序包:

Nancy.Authentication.Forms Nancy.Hosting.Aspnet Nancy.Validation.FluentValidation Nancy.Viewengines .Razor

回答

3

確保你的配置看起來有點像這樣

<configuration> 
    <configSections> 
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" /> 
    </configSections> 
    <appSettings> 
    <add key="webPages:Enabled" value="false" /> 
    </appSettings> 
    <system.web> 
    <compilation> 
     <buildProviders> 
     <add extension=".cshtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyCSharpRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" /> 
     <add extension=".vbhtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyVisualBasicRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" /> 
     </buildProviders> 
    </compilation> 
    </system.web> 
    <razor disableAutoIncludeModelNamespace="false"> 
    <assemblies> 
     <add assembly="Nancy" /> 
    </assemblies> 
    <namespaces> 
     <add namespace="Nancy" /> 
     <add namespace="Nancy.ViewEngines.Razor" /> 
    </namespaces> 
    </razor> 
</configuration> 

它會工作

+0

這很好,謝謝! – Pelle 2013-04-12 07:12:41