小编Gur*_*ley的帖子

ASP.NET Core 3.1:共享本地化不适用于 3.1 版

我可能没有在startup.cs文件中进行正确的配置。我创建了一个演示应用程序以使其正常工作,但是在尝试了各种操作后它不起作用。演示存储库可在以下链接中获得

https://github.com/gurpreet42/MyAppV3

startup.cs 文件的配置是

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<LocService>();
   services.AddLocalization(options => options.ResourcesPath = "Resources");

   services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new List<CultureInfo>
                                            {
                                                new CultureInfo("en-US"),
                                                new CultureInfo("nl")
                                            };

                options.DefaultRequestCulture = new RequestCulture("en-US");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });

   services.AddMvc()
           .AddViewLocalization()
           .AddDataAnnotationsLocalization(options =>
                {
                   options.DataAnnotationLocalizerProvider = (type, factory) =>
                   {
                       var assemblyName = new AssemblyName(typeof(SharedResource).GetTypeInfo().Assembly.FullName);
                       return factory.Create("SharedResource", assemblyName.Name);
                   };
               }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app,
                        IHostingEnvironment env,
                        ILoggerFactory loggerFactory)
{
    // Localisation
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value); …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core asp.net-core

9
推荐指数
2
解决办法
5095
查看次数

Serilog:如何在 asp.net 核心的“类库”中使用日志记录

我正在使用 Serilog 登录 asp.net 核心网站,并且项目中使用了“类库”。

  1. 如何在asp.net核心类库中实现登录?
  2. 有没有办法在“类库”中独立实现日志记录?

serilog asp.net-core-1.0

4
推荐指数
1
解决办法
2114
查看次数

Serilog:RollingFile 在带有“appsettings.json”的 asp.net 核心中不起作用

我正在使用 asp.net core(website) 和 Serilog 进行日志记录。

安装的 Nuget 包是

Serilog
Serilog.AspNetCore
Serilog.Extensions.Logging
Serilog.Extensions.Logging.File
Serilog.Settings.Configuration
Serilog.Sinks.File
Serilog.Sinks.RollingFile
Microsoft.Extensions.Logging
Run Code Online (Sandbox Code Playgroud)

Main.cs 文件中的设置是:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
             .ConfigureAppConfiguration((WebHostBuilderContext, configurationBuilder) =>
             {
                 configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                 configurationBuilder.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true);
                 configurationBuilder.AddEnvironmentVariables();
             })
             .ConfigureLogging((hostingContext, loggingBuilder) =>
             {
                 loggingBuilder.ClearProviders();
                 loggingBuilder.AddSerilog();
             })
            .UseStartup<Startup>();  
}
Run Code Online (Sandbox Code Playgroud)

什么工作:

在 Startup.cs 文件中,如果我编写以下代码,则会创建日志文件。在这里,我在代码本身中指定配置。

  public void Configure(IApplicationBuilder app,
                                IHostingEnvironment env,
                                ILoggerFactory loggerFactory)
    {
     string logFilePath = Path.Combine(env.ContentRootPath, …
Run Code Online (Sandbox Code Playgroud)

c# logging serilog asp.net-core

3
推荐指数
1
解决办法
2343
查看次数

GetHashCode方法在asp.net core中为同一字符串返回不同的值

我在应用程序中编写了以下代码。

  var salt = "0115ee62e6e5e8ea68db9e2d7e72cec85e810ad9566212ffc2c4b76a17ca0b00";
  var hashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(salt ?? string.Empty);
Run Code Online (Sandbox Code Playgroud)

它在“asp.net core”和“标准”框架中具有不同的行为。

框架 4.7: 如果我在 .net 标准框架中创建的网站/控制台应用程序中使用此代码,它总是会给我相同的结果。hashcode 变量的值始终为“1066337973”。

Asp.net Core: 如果我将在 asp.net core 网站中使用此代码,那么每次我停止应用程序并再次启动时,hashcode 变量都有不同的值。

由于这个问题,我的逻辑在 ASP.NET Core 应用程序中失败了。
这个问题如何解决?

.net c# frameworks .net-core asp.net-core

3
推荐指数
1
解决办法
1171
查看次数

如何在asp.net core中获取IIS版本

我正在使用 asp.net 核心 2.0。

在服务器端的 c# 代码中,我想检查托管网站的 IIS 版本。

在 asp.net 的早期,以下行为我们提供了 iis 版本。

HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"];
Run Code Online (Sandbox Code Playgroud)

它给了我像Microsoft-IIS/10.0这样的结果

如何在asp.net core 中得到想要的结果?

asp.net-core

2
推荐指数
1
解决办法
888
查看次数