asp运行局域网访问
2022/6/1小于 1 分钟
asp运行局域网访问
注意:
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;
}
}
