You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
9.9 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

项目说明
=======
项目主要学习WebAPI启动项设置特别是默认启始页设置。特别是在部署为子站点时的默认启动页。
常用方法:
+ 路由+重定向
```csharp
//6.0及以上版本
app.Map("/", async context =>
{
//重定向到目录位置
context.Response.Redirect("swagger/index.html");
await Task.CompletedTask;
});
//或者MapGet方法
//老版本写法
app.UseEndpoints(endpoints =>
{
//默认打开Swagger
endpoints.Map("/", async context =>
{
context.Response.Redirect($"{context.Request.PathBase}/swagger/index.html");
await Task.CompletedTask;
});
});
```
+ 路由+自定义内容
```csharp
namespace LaunchUrlStudy.SubSite2
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.MapGet("/", async context =>
{
//绝对Url
string swaggerUrl = Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildAbsolute(context.Request.Scheme, context.Request.Host, context.Request.PathBase, "/swagger/index.html");
//相对Url
string swaggerUrl2 = Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildRelative(context.Request.PathBase, "/swagger/index.html");
context.Response.ContentType = "text/html";
var wellcomHtml =
$"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{AppDomain.CurrentDomain.FriendlyName}</title>
</head>
<body>
<div style="text-align:center">
<h2>{AppDomain.CurrentDomain.FriendlyName} 启动成功!</h2>
<h3>详情参阅:<a href="{swaggerUrl}">Swagger 文档</a></h3>
</div>
</body>
</html>
""";
await context.Response.WriteAsync(wellcomHtml);
});
app.Run();
}
}
}
```
+ 路由+html文件
```csharp
namespace LaunchUrlStudy.SubSite3
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// 配置IoC
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// 配置请求管道
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.Map("/", async context =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(app.Environment.ContentRootPath, "index.html"));
});
app.Run();
}
}
}
```
+ 自定义中间件+重定向
```csharp
namespace LaunchUrlStudy.SubSite4
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
//配置启动地址(支持子站点):自定义中间件+重定向
app.Use(async (context, next) =>
{
if (context.Request.Path == "/" || context.Request.Path == "")
{
context.Response.Redirect($"{context.Request.PathBase}/swagger/Index.html");
}
else
{
await next();
}
});
app.Run();
}
}
}
```
+ 第三方中间件:`Swagger中间件` 子站点时,末尾必须是/才能正确
```csharp
namespace LaunchUrlStudy.SubSite5
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
// 配置启动地址(不支持子站点):设置Swagger中间件
app.UseSwaggerUI(config =>
{
config.SwaggerEndpoint("swagger/v1/swagger.json", "My API V1");
config.RoutePrefix = string.Empty;
});
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
```
+ 第三方中间件:`健康检查中间件` 即默认为健康检查地址
```csharp
namespace LaunchUrlStudy.SubSite6
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddHealthChecks();
var app = builder.Build();
app.UseAuthorization();
app.MapControllers();
//配置启动地址(支持子站点):第三方 `健康检查中间件`
app.UseHealthChecks("");
app.Run();
}
}
}
```
+ 使用静态文件服务器UseDefaultFiles和UseStaticFiles
```csharp
namespace LaunchUrlStudy.SubSite7
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
// 配置启动地址(支持子站点):静态文件服务器默认页
// 只能是wwwroot里的静态真实文件不能是"swagger/index.html"(因为它是㠌入的资源文件不在wwwroot中)
app.UseDefaultFiles(new DefaultFilesOptions()
{
DefaultFileNames = new[]
{
//可以是静态文件
"index.html",
//不能是虚拟文件因为它是㠌入的资源文件不在wwwroot中
//"swagger/index.html"
}
});
app.UseStaticFiles();
app.Run();
}
}
}
```
+ 环境变量与配置文件:不推荐
思路是:在配置或环境变量中设置一个指定的变量,在程序代码中读取其值后,配置使用。
这需要事先知道子站点的值不灵活。环境变量可以在Docker中使用配置文件就太不灵活了。
+ 部署配置:不推荐
在部署 ASP.NET 6 Web API时配置服务器或反向代理服务器如Nginx或Apache来处理默认请求。例如在Nginx配置中设置默认页面
```lua
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
```
+ 使用URL重写规则不推荐
在使用IIS或Nginx等软件代理时可以设置URL重写规则。因为需要引入组件和配置规则相对来说太麻烦不推荐使用。
总结:
+ 优先使用: 自定义路由(重定向或自定义内容或文件) 和 自定义中间件,简单、灵活、支持站点也支持子站点;
+ 如果启用了静态站点服务,可以配置 UseDefaultFiles 可以指定静态站点中的html文档
+ 如果默认地址,用作 `健康检查路径`,也可以直接设置`健康检查路径`为站点首页;
+ 其它方式不是不灵活就是太麻烦不推荐使用。Swagger设置为首页时也推荐使用 自定义路由或中间件;