小编mrf*_*eck的帖子

在单独的项目Asp.net Core MVC中进行本地化

我刚刚升级到Rc2,而以前的工作不再适用.我在一个单独的项目中有几个resx文件,我使用自定义类来访问数据.现在运行时出现以下错误:

MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源.确保在编译时将"GarageWeb.Core.CoreResources.resources"正确嵌入或链接到程序集"GarageWeb.Core"中,或者所有所需的附属程序集都是可加载和完全签名的.

编辑:我简化了这一点,并创建了一个控制台应用程序,除了重现错误所需的一切,所有内容都被剥夺了:https://github.com/GarageWeb/ResourceTest

这是访问资源的类:

public  class ResourceService : IResourceService
{
    private readonly ILoggingService _loggingService;
    private readonly ICoreGlobalResourceService _coreGlobalResources;
    private readonly ISiteGlobalResourceService _siteGlobalResources;
    public ResourceService(ILoggingService loggingService, ICoreGlobalResourceService coreGlobalResourceService, ISiteGlobalResourceService siteGlobalResources)
    {
        _loggingService = loggingService;
        _coreGlobalResources = coreGlobalResourceService;
        _siteGlobalResources = siteGlobalResources;
    }
    public  string GetGlobalText(string resourceKey, bool includeBrackets = true)
    {
        var localizedString = _coreGlobalResources.ResourceManager.GetString(resourceKey);
        if (string.IsNullOrEmpty(localizedString))
        {
            localizedString = _siteGlobalResources.ResourceManager.GetString(resourceKey);
        }
        if (string.IsNullOrEmpty(localizedString) && includeBrackets)
        {
           _loggingService.LogInvalidResource(resourceKey);
        }

        if (includeBrackets)
        {
            return localizedString ?? "[" + resourceKey + …
Run Code Online (Sandbox Code Playgroud)

c# .net-core asp.net-core asp.net-core-1.0 asp.net-core-localization

7
推荐指数
1
解决办法
5826
查看次数

注入 IHttpContextAccessor 中的 HttpContext null

我有一个授权处理程序,我正在将自定义 personService 注入其中。我已经通过默认 DI 连接了处理程序和 personService。人员服务 IhttpContextAccessor 在我的应用程序的其他地方工作正常,但是当从授权处理程序调用它时,httpcontext 始终为 null。

这是 AuthHandler:

     public class NoChildrenRequirement : AuthorizationHandler<NoChildrenRequirement>, IAuthorizationRequirement
{
    private readonly IPersonService _personService;

    public NoChildrenRequirement(IPersonService personService)
    {
        _personService = personService;
    }

    protected override void Handle(AuthorizationContext context, NoChildrenRequirement requirement)
    {
        var user = context.User;
        var identity = user.Identity;
        var isAuthenticated = identity.IsAuthenticated;

        var currentPerson = _personService.CurrentPerson();

        if (isAuthenticated && currentPerson != null && !currentPerson.IsAChild)
        {
            context.Succeed(requirement);
            return;
        }
        context.Fail();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我在启动时连接它的地方:

     services.AddTransient<NoChildrenRequirement>();
        services.Configure<AuthorizationOptions>(options =>
        {
            options.AddPolicy("NoChildren", policy => policy.Requirements.Add(
                services.BuildServiceProvider().GetRequiredService<NoChildrenRequirement>())); …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-mvc asp.net-core

5
推荐指数
0
解决办法
2466
查看次数