ASP.NET 运行局域网访问
2022/6/1小于 1 分钟
ASP.NET 运行局域网访问
注意
注意:launchSettings.json 在开发环境优先级高于 appsettings.json
打开 properties 文件夹下面的 launchSettings.json 然后使用 "applicationUrl": "http://0.0.0.0:5059"。
下面是例子:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:15455",
"sslPort": 0
}
},
"profiles": {
"SimpleApi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://0.0.0.0:5059",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
打包 EXE 设置端口(硬编码)
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:3045");
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
打包后配置 URL
在 appsettings.json 设置 URL:
{
"appName": "mock name",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Urls": "http://*:5411"
}
前提是没有在代码中硬编码:
builder.WebHost.UseUrls("http://*:3045");
读取 appsettings.json 的配置
ConfService.cs
public class ConfService
{
private readonly IConfiguration _cfg;
public ConfService(IConfiguration cfg) => _cfg = cfg;
public void Foo()
{
// 支持冒号分层
string conn = _cfg["BookStoreDatabase:ConnectionString"];
string db = _cfg["appName"];
Console.WriteLine(db);
}
}
Program.cs
builder.Services.AddSingleton<ConfService>();
然后在 Controller 使用
[ApiController]
[Route("/index")]
public class IndexController : ControllerBase
{
private readonly ConfService _confService;
public IndexController(ConfService confService)
{
_confService = confService;
}
[HttpGet("/count")]
public int Index()
{
_confService.Foo();
return 1222;
}
}
