Русские символы в ASP.Net Core
Код "сервера":
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Server
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Съешь ещё этих мягких французских булок, да выпей же чаю.");
});
});
}
}
}
Но "сервер" выхлопнул не то, что мне нужно:

Как заставить ASP.Net Core отправлять русские символы?
Ответы (3 шт):
Автор решения: Exploding Kitten
→ Ссылка
Попробуйте добавить в заголовки кодировку:
endpoints.MapGet("/", async context =>
{
context.Response.Headers["Content-Type"] = "text/plain; charset=utf-8";
// ...
});
Автор решения: Evgeny20
→ Ссылка
Или так, можно было получить доступ к свойству ContentType объекта HttpResponse напрямую, а не через заголовки:
response.ContentType = "text/plain; charset=utf-8";
Тоже самое, что и в ответе выше, только через обращение напрямую к свойству ContentType, а не индексатор:
response.Headers.ContentType = "text/plain; charset=utf-8";
Автор решения: Геннадий Юрьев
→ Ссылка
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.WebEncoders;
namespace Mywebsite2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime.
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.Configure<WebEncoderOptions>(options =>
{
options.TextEncoderSettings = new System
.Text.Encodings.Web
.TextEncoderSettings(System.Text.Unicode.UnicodeRanges.All);
});
}
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days.
// You may want to change this for production scenarios,
// see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
WebEncoderOptions добавлял я - без этой штуки в браузере были кракозяблики вместо русских букв, английский отображался нормально.