找不到使用swagger的.NET Core 3.1 API的/swagger/WaterMasterDataOpenApiSpecification/swagger.json获取错误
如何帮助Swagger查找其文档?我读过多篇关于招摇文档的帖子。基本上,我的问题是我可以让Swagger在本地主机上工作,但一旦我们的API部署到服务器上,我就无法让Swagger工作,当我试图让Swagger在开发、测试和生产服务器上工作时,我在本地主机上破坏了它找不到使用swagger的.NET Core 3.1 API的/swagger/WaterMasterDataOpenApiSpecification/swagger.json获取错误,swagger,asp.net-core-3.1,swashbuckle.aspnetcore,Swagger,Asp.net Core 3.1,Swashbuckle.aspnetcore,如何帮助Swagger查找其文档?我读过多篇关于招摇文档的帖子。基本上,我的问题是我可以让Swagger在本地主机上工作,但一旦我们的API部署到服务器上,我就无法让Swagger工作,当我试图让Swagger在开发、测试和生产服务器上工作时,我在本地主机上破坏了它 public void ConfigureServices(IServiceCollection services) { services .AddCors(
public void ConfigureServices(IServiceCollection services)
{
services
.AddCors()
.AddDbContext<OurDbContext1>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase1"]))
.AddDbContext<OurDbContext2>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase2"]))
.AddRazorPages()
.ConfigureApiBehaviorOptions(setupAction =>
{
setupAction.InvalidModelStateResponseFactory = context =>
{
var problemDetails = new ValidationProblemDetails(context.ModelState)
{
Title = "One or more model validation errors occurred",
Status = StatusCodes.Status422UnprocessableEntity,
Detail = "See the errors property for details",
Instance = context.HttpContext.Request.Path
};
problemDetails.Extensions.Add("traceId", context.HttpContext.TraceIdentifier);
return new UnprocessableEntityObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" }
};
};
});
services
.AddMvc(options => options.EnableEndpointRouting = false)
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services
.AddControllersWithViews();
services
.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services
.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc("OurApiSpecification", new OpenApiInfo()
{
Title = "Our API",
Version = "1"
});
setupAction.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
services
.AddScoped<IRepository1, Repository1>()
.AddScoped<IRepository2, Repository2>()
.AddTransient<IPropertyMappingService, PropertyMappingService>()
.AddTransient<IPropertyCheckingService, PropertyCheckingService>();
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
if (_env.IsDevelopment())
{
services.AddSingleton<IPolicyEvaluator, DisableAuthenticationPolicyEvaluator>();
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
string[] allowedOrigins = Configuration.GetSection("CorsSettings:AllowedOrigins").Get<string[]>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
app.UseCors(
options => options
.WithOrigins(
allowedOrigins
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
if (!env.IsDevelopment())
{
app.UseAuthentication();
}
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(setupAction =>
{
setupAction.SwaggerEndpoint(
"/swagger/OurApiSpecification/swagger.json",
"Our API");
});
app.UseMvc();
}
下面是当Swagger在本地主机上工作时,my Startup.cs中的代码
public void ConfigureServices(IServiceCollection services)
{
services
.AddCors()
.AddDbContext<OurDbContext1>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase1"]))
.AddDbContext<OurDbContext2>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase2"]))
.AddRazorPages()
.ConfigureApiBehaviorOptions(setupAction =>
{
setupAction.InvalidModelStateResponseFactory = context =>
{
var problemDetails = new ValidationProblemDetails(context.ModelState)
{
Title = "One or more model validation errors occurred",
Status = StatusCodes.Status422UnprocessableEntity,
Detail = "See the errors property for details",
Instance = context.HttpContext.Request.Path
};
problemDetails.Extensions.Add("traceId", context.HttpContext.TraceIdentifier);
return new UnprocessableEntityObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" }
};
};
});
services
.AddMvc(options => options.EnableEndpointRouting = false)
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services
.AddControllersWithViews();
services
.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services
.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc("OurApiSpecification", new OpenApiInfo()
{
Title = "Our API",
Version = "1"
});
setupAction.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
services
.AddScoped<IRepository1, Repository1>()
.AddScoped<IRepository2, Repository2>()
.AddTransient<IPropertyMappingService, PropertyMappingService>()
.AddTransient<IPropertyCheckingService, PropertyCheckingService>();
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
if (_env.IsDevelopment())
{
services.AddSingleton<IPolicyEvaluator, DisableAuthenticationPolicyEvaluator>();
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
string[] allowedOrigins = Configuration.GetSection("CorsSettings:AllowedOrigins").Get<string[]>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
app.UseCors(
options => options
.WithOrigins(
allowedOrigins
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
if (!env.IsDevelopment())
{
app.UseAuthentication();
}
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(setupAction =>
{
setupAction.SwaggerEndpoint(
"/swagger/OurApiSpecification/swagger.json",
"Our API");
});
app.UseMvc();
}
public void配置服务(IServiceCollection服务)
{
服务
.AddCors()
.AddDbContext(选项=>
options.UseSqlServer(配置[“ConnectionString:OurDatabase1]”)
.AddDbContext(选项=>
options.UseSqlServer(配置[“ConnectionString:OurDatabase2]”)
.AddRazorPages()
.ConfigureApiBehaviorOptions(设置操作=>
{
setupAction.InvalidModelStateResponseFactory=上下文=>
{
var problemDetails=new ValidationProblemDetails(context.ModelState)
{
Title=“发生了一个或多个模型验证错误”,
状态=状态代码。状态422不可处理实体,
Detail=“有关详细信息,请参阅errors属性”,
实例=context.HttpContext.Request.Path
};
problemDetails.Extensions.Add(“traceId”,context.HttpContext.TraceIdentifier);
返回新的UnprocessableEntityObjectResult(problemDetails)
{
ContentTypes={“应用程序/问题+json”}
};
};
});
服务
.AddMvc(选项=>options.EnableEndpointRouting=false)
.AddNewtonsoftJson(选项=>options.SerializerSettings.ContractResolver=new DefaultContractResolver());
服务
.AddControllersWithViews();
服务
.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblys());
服务
.AddSwaggerGen(setupAction=>
{
setupAction.SwaggerDoc(“OurApiSpecification”,新的openapinfo()
{
Title=“我们的API”,
Version=“1”
});
setupAction.ResolveConflictingActions(apisDescriptions=>apisDescriptions.First());
});
服务
.addScope()文件
.addScope()文件
.AddTransient()
.AddTransient();
服务
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
如果(_env.IsDevelopment())
{
services.AddSingleton();
}
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
string[]allowedOrigins=Configuration.GetSection(“CorsSettings:allowedOrigins”).Get();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
app.UseCors(
选项=>选项
.有来历(
允许起源
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
如果(!env.IsDevelopment())
{
app.UseAuthentication();
}
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(setupAction=>
{
setupAction.SwaggerEndpoint(
“/swagger/OurApiSpecification/swagger.json”,
“我们的API”);
});
app.UseMvc();
}
但是,当我将API部署到服务器并尝试通过URL访问它时,我看到了Fetch错误Not Found/swagger/OurApiSpecification/swagger.json,这是有意义的,因为服务器上的API位于与本地不同的主机名和路径
服务器上的serverhostname/apipfolder/ourapi/swagger/OurApiSpecification/swagger.JSON上存在JSON文档,但错误告诉我,在Startup.cs中指定的位置找不到它,这也是有意义的,因为文档不在/swagger/OurApiSpecification/swagger.JSON,但它位于/apifolder/ourapi/swagger/OurApiSpecification/swagger.json
因此,经过广泛的研究,我更改了Startup.Configure(),试图在服务器上的位置引用Swagger JSON文档,并且,在Startup.ConfigureServices()中,我加入了Swagggen Newtonsoft支持,但现在Swagger无法在localhost上工作
现在,这里是我的Startup.cs中的代码,当Swagger在localhost上不工作时
public void ConfigureServices(IServiceCollection services)
{
services
.AddCors()
.AddDbContext<OurDbContext1>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase1"]))
.AddDbContext<OurDbContext2>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:OurDatabase2"]))
.AddRazorPages()
.ConfigureApiBehaviorOptions(setupAction =>
{
setupAction.InvalidModelStateResponseFactory = context =>
{
var problemDetails = new ValidationProblemDetails(context.ModelState)
{
Title = "One or more model validation errors occurred",
Status = StatusCodes.Status422UnprocessableEntity,
Detail = "See the errors property for details",
Instance = context.HttpContext.Request.Path
};
problemDetails.Extensions.Add("traceId", context.HttpContext.TraceIdentifier);
return new UnprocessableEntityObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" }
};
};
});
services
.AddMvc(options => options.EnableEndpointRouting = false)
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services
.AddControllersWithViews();
services
.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services
.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc("OurApiSpecification", new OpenApiInfo()
{
Title = "Our API",
Version = "1"
});
setupAction.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
services
.AddSwaggerGenNewtonsoftSupport();
services
.AddScoped<IRepository1, Repository1>()
.AddScoped<IRepository2, Repository2>()
.AddTransient<IPropertyMappingService, PropertyMappingService>()
.AddTransient<IPropertyCheckingService, PropertyCheckingService>();
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
if (_env.IsDevelopment())
{
services.AddSingleton<IPolicyEvaluator, DisableAuthenticationPolicyEvaluator>();
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
string[] allowedOrigins = Configuration.GetSection("CorsSettings:AllowedOrigins").Get<string[]>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
app.UseCors(
options => options
.WithOrigins(
allowedOrigins
)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
if (!env.IsDevelopment())
{
app.UseAuthentication();
}
app.UseAuthorization();
app.UseSwagger(setupAction =>
{
setupAction.RouteTemplate =
"apifolder/ourapi/swagger/OurApiSpecification/swagger.json";
});
app.UseSwaggerUI(setupAction =>
{
setupAction.SwaggerEndpoint(
"/apifolder/ourapi/swagger/OurApiSpecification/swagger.json",
"Our API");
setupAction.RoutePrefix = "apifolder/ourapi/swagger";
});
app.UseMvc();
}
public void配置服务(IServiceCollection服务)
{
服务
.AddCors()
.AddDbContext(选项=>
options.UseSqlServer(配置[“ConnectionString:OurDatabase1]”)
.AddDbContext(选项=>
options.UseSqlServer(配置[“ConnectionString:OurDatabase2]”)
.AddRazorPages()
.ConfigureApiBehaviorOptions(设置操作=>
{
setupAction.InvalidModelStateResponseFactory=上下文=>
{
var problemDetails=new ValidationProblemDetails(context.ModelState)
{
Title=“发生了一个或多个模型验证错误”,
状态=状态代码。状态422不可处理实体,
Detail=“有关详细信息,请参阅errors属性”,
实例=context.HttpContext.Request.Path
};
problemDetails.Extensions.Ad