gpt4 book ai didi

c# - Nancy 的 Html.Render Context.Context.CurrentUser 抛出异常

转载 作者:行者123 更新时间:2023-11-30 22:18:44 25 4
gpt4 key购买 nike

我是第一次试用 Nancy(到目前为止我真的很喜欢它)并且遇到了一些问题。当我调用 Html.RenderContext.Context.CurrentUser 时出现以下错误:

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)

该 View 是一个局部 View ,如下所示:

<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>
}

在 Bootstrap 中启用了 FormsAuthentication

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

Usermapper 和 Identity 实现如下所示

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南希.Hosting.Aspnet南希.Validation.FluentValidation南希.Viewengines.Razor

最佳答案

确保你的配置看起来有点像这样

<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>

它会起作用

关于c# - Nancy 的 Html.Render Context.Context.CurrentUser 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com