c# - Kestrel 从 VS2019 开始:页面正常,从可执行文件开始:404
问题描述
刚刚创建了 asp.Net 核心项目。
- 从 VS2019 开始:我有请求的页面。
- 从调试可执行文件:找不到页面错误 404。
在两者上使用:http://localhost:5555/swagger/index.html。
=================================================
不起作用(可执行直接)
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://0.0.0.0:5555
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Prj\Personnel\WallyRest\WallyRest\bin\Debug\net5.0
=================================================
工作正常(从 Visual Studio 2019 开始:调试 - 任何 CPU)
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Overriding address(es) 'http://localhost:5115'. Binding to endpoints defined in UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://0.0.0.0:5555
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Prj\Personnel\WallyREst\WallyRest
=================================================
应用设置.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5555" // ";http://pd140356:8181;http://*:6666",
// "Url": "http://localhost:5555"
}
}
},
"AllowedHosts": "*",
// "urls": "http://*:5115;http://*:8888"
}
=================================================
我没有 appsettings.Production.json
appsettings.Development.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
=================================================
配置服务和配置代码:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WallyRest", Version = "v1" });
});
}
// 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.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
解决方案
您看到的问题归结为以下代码:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));
}
当您从 Visual Studio 运行代码时,它通常会将ASPNETCORE_ENVIRONMENT
环境变量设置为“开发”:
当应用程序启动时,这反过来将代码中的环境设置为“开发”。如果未设置此值,则默认为“生产”
最终这意味着if (env.IsDevelopment())
它将评估为false
,因为它不再是开发环境,并且块中的代码将不会运行。
解决方案是简单地将 Swagger 代码移到此块之外:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WallyRest v1"));
推荐阅读
- puppet - 从 puppet master 向代理共享凭据
- node.js - 如何从 socket.io 仅发送授权域的消息
- c# - 使用 ToAsyncList() 查询数据库后出现 NullReferenceException,但使用 ToList() 时不会出现 NullReferenceException
- html - 当我尝试插入图像时,边框和输入会移动
- java - 设置标签固定大小图像来自数据库
- html - 放大和缩小时按钮的位置发生变化:CSS
- r - 如何将我的一个列的值范围分配给 ggplot2 中的一组颜色?
- python - 使用“Hydrogen”在 Atom 中运行代码时出错
- python-3.x - Pymongo 查询日期失败
- regression - 使用 estout/esttab 为 Stata 中的交互创建回归表