i 试图将蜂窝项目与......连接起来。 NET 7 AP。
允许所有来源的代码(startup.cs
)是:
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
该法典通过手机向我的复印机(同一计算机上的手机和直网ar)提供包裹。
今天,我试图使同样的法典化,或至少允许只进入单人地址,因为现在没有成功。
这是<编码>program.cs中的代码。 i 试图操作:
builder.Services.AddCors(options =>
{
var MyAllowSpecificOrigins = "_MyAllowSubdomainPolicy";
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:4200")
.SetIsOriginAllowedToAllowWildcardSubdomains();
});
});
之后在中文本部分:
app.UseCors();
app.UseAuthorization();
i 仍然有CORS的错误,没有来自该网的任何答复头。
<>概要>:第7版的对应代码允许任何来源并避免CORS错误?
edited after Qing Guo response:
CORS的错误仍然存在: 我随函附上所有可能有助于的资料和守则。
页: 1
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using myProjAPI.Data;
using Microsoft.AspNetCore.HttpOverrides;
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:4200")
.SetIsOriginAllowedToAllowWildcardSubdomains();
});
});
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI
https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<Context>(options => options
.UseSqlServer(builder.Configuration
.GetConnectionString("myProject")));
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseForwardedHeaders();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseCors(MyAllowSpecificOrigins);
app.UseAuthorization();
app.MapControllers();
app.Run();
页: 1 Chrome DevTools:
https://i.stack.imgur.com/YZI6B.jpg”rel=“nofollow noreferer”>dev instruments rec0.1
https://i.stack.imgur.com/u2r63.jpg”rel=“nofollow noreferer”>dev instruments rec0.2
https://i.stack.imgur.com/wvYMM.jpg”rel=“nofollow noreferer”>dev instruments rec0.3
这是我的<代码>发射。
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:29309",
"sslPort": 44358
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7106;http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
感谢!