Я следовал рекомендациям, перечисленным в статье Microsoft: Включить запросы кросс-происхождения (CORS) в ASP. NET Core , и я по-прежнему не могу получить доступ к API из локального vue веб-сайт или PostMan. Любые предложения?
Вот что определено в AllowedHosts:
"AllowedHosts": "http://localhost;http://localhost:8080"
Вот класс запуска:
using App.Core.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace App.Core
{
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.AddDbContext<ImportContext>((builder) =>
{
builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
});
services.AddDbContext<CodeAdminContext>((builder) =>
{
builder.UseSqlServer(Configuration.GetConnectionString("ParsingAppDb"));
});
services.AddScoped(typeof(IImportContext), typeof(ImportContext));
services.AddScoped(typeof(ICodeAdminContext), typeof(CodeAdminContext));
services.AddTransient(typeof(Logic.IImporter), typeof(Logic.Importer));
services.AddTransient(typeof(Logic.I2964Procssor), typeof(Logic.Processor_2964));
services.AddTransient(typeof(Logic.I2965Procssor), typeof(Logic.Processor_2965));
var allowedHosts = Configuration.GetValue(typeof(string), "AllowedHosts") as string;
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
if (allowedHosts == null || allowedHosts == "*")
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
return;
}
string[] hosts;
if (allowedHosts.Contains(';'))
hosts = allowedHosts.Split(';');
else
{
hosts = new string[1];
hosts[0] = allowedHosts;
}
builder.WithOrigins(hosts)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
}); services.AddControllers();
}
// 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.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Даже использование fiddler не показывает ничего полезного, просто отказанный звонок. И наоборот, если я установлю его на любое происхождение, я смогу заставить PostMan работать, но веб-сайт vue затем вернет, что Accept-Control не был установлен.
Обновление: и у меня уже установлен пакет Microsoft.AspNetCore.Cors .