ASP.NET Core 응용 프로그램이 호스팅되는 포트를 지정하는 방법은 무엇입니까?
을 할 때WebHostBuilder순식간에Main진입점, 바인딩되는 포트를 어떻게 지정할 수 있습니까?
기본적으로 5000을 사용합니다.
이 질문은 새 ASP.NET Core API(현재 1.0.0-RC2에 있음)와 관련이 있습니다.
ASP.NET Core 3.1에서는 사용자 지정 포트를 지정하는 네 가지 주요 방법이 있습니다.
- 인수를 프로그램을 .NET 응용 프로그램을 합니다.
--urls=[url]:
dotnet run --urls=http://localhost:5001/
- 용사를 합니다.
appsettings.json를Urls스캐너:
{
"Urls": "http://localhost:5001"
}
- 할 때 하기, 사용하기
ASPNETCORE_URLS=http://localhost:5001/. - 용사를 합니다.
UseUrls()프로그래밍 방식으로 수행하려는 경우:
public static class Program
{
public static void Main(string[] args) =>
CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
builder.UseUrls("http://localhost:5001/");
});
}
또는 일반 호스트 작성기 대신 웹 호스트 작성기를 계속 사용하는 경우:
public class Program
{
public static void Main(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://localhost:5001/")
.Build()
.Run();
}
asp.net core 2.1+ appsettings.json 파일에 Kestrel 섹션을 삽입할 수 있습니다.
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
}
}
},
kestrel 섹션이 없는 경우 "kestrel"을 사용할 수 있습니다.
{
"urls":"http://*.6001;https://*.6002"
}
그러나 appsettings.json에 kestrel이 있으면 URL 섹션이 실패합니다.
VS 도커 통합을 통해 이를 수행하는 모든 사용자에게 도움이 되는 후속 답변을 제공합니다.구글 앱 엔진에서 "유연한" 환경을 사용하여 실행하려면 포트 8080으로 변경해야 했습니다.
도커 파일에 다음이 필요합니다.
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
도커-docker.yml의 포트도 수정해야 합니다.
ports:
- "8080"
앱을 변경하지 않고 호스팅 URL을 지정할 수 있습니다.
성을 합니다.Properties/launchSettings.json프로젝트 디렉토리에 파일을 저장하고 다음과 같은 내용으로 채웁니다.
{
"profiles": {
"MyApp1-Dev": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5001/"
}
}
}
dotnet run는 당신의 명은당선합니다야해택을 해야 합니다.launchSettings.json콘솔에 파일이 표시됩니다.
C:\ProjectPath [master ≡]
λ dotnet run
Using launch settings from C:\ProjectPath\Properties\launchSettings.json...
Hosting environment: Development
Content root path: C:\ProjectPath
Now listening on: http://localhost:5001
Application started. Press Ctrl+C to shut down.
더 자세한 정보: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments
.net core 2.2 이상의 방법 WebHost를 사용한 메인 지원 인수.기본 작성기(인수) 작성
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
프로젝트를 빌드하고 다음으로 이동할 수 있습니다.bin과 같은 합니다.
dotnet <yours>.dll --urls=http://0.0.0.0:5001
또는 멀티 스레드와 함께
dotnet <yours>.dll --urls="http://0.0.0.0:5001;https://0.0.0.0:5002"
2021/09/14 편집
. 3이후에는 .net core 3.1 할 수 .appsettings.json section 로프 구성 섹션Urls그리고.Kestrel 둘중 할 수 있습니다.둘 중 하나를 사용할 수 있습니다. Urls더 쉬워질 것입니다.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"MicrosoftHostingLifetime": "Information"
}
},
"Urls": "http://0.0.0.0:5002",
//"Kestrel": {
// "EndPoints": {
// "Http": {
// "Url": "http://0.0.0.0:5000"
// },
// "Https": {
// "Url": "https://0.0.0.0:5001"
// }
// }
//},
"AllowedHosts": "*"
}
사용하다http://0.0.0.0:5000연결을 웹 수 . 를 " " " " " 으로 하면 (" " " " " " " " )로 설정됩니다.http://localhost:5000컴퓨터에서만 액세스할 수 있습니다.
만들기 위해서Kestrel, 은 정작업변, 코를야합니에서 .Program.cs프로젝트에서.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(context.Configuration.GetSection("Kestrel"));
});
webBuilder.UseStartup<Startup>();
});
대안적인 해결책은 다음과 같습니다.hosting.json프로젝트의 근저에서.
{
"urls": "http://localhost:60000"
}
에 그음에다.Program.cs
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", true)
.Build();
var host = new WebHostBuilder()
.UseKestrel(options => options.AddServerHeader = false)
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
사용하는 경우dotnet run
dotnet run --urls="http://localhost:5001"
도커 컨테이너(Linux 버전)에서 호스팅할 때 '연결 거부됨' 메시지가 표시될 수 있습니다.이 경우 로컬 호스트 루프백 대신 "이 시스템의 모든 IP 주소"를 의미하는 IP 주소 0.0.0을 사용하여 포트 포워딩을 수정할 수 있습니다.
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseUrls("http://0.0.0.0:5000/")
.Build();
host.Run();
}
}
.Net Core 3.1에서 Microsoft Doc을 따라가면 매우 간단합니다. kestrel-aspnetcore-3.1
요약:
아래 서비스 구성 섹션을 CreateDefaultBuilder에 추가합니다.
Program.cs:// using Microsoft.Extensions.DependencyInjection; public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { services.Configure<KestrelServerOptions>( context.Configuration.GetSection("Kestrel")); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });다음 기본 구성 추가
appsettings.json파일(Microsoft 문서의 추가 구성 옵션):"Kestrel": { "EndPoints": { "Http": { "Url": "http://0.0.0.0:5002" } } }프로젝트 게시/디버그/릴리스 바이너리 폴더에서 CMD 또는 콘솔을 열고 다음을 실행합니다.
dotnet YourProject.dllhttp://localhost:5002에서 사이트/api 탐색을 즐기십시오.
또는 명령줄을 통해 앱을 실행하여 포트를 지정할 수 있습니다.
단순 실행 명령:
dotnet run --server.urls http://localhost:5001
참고: 여기서 5001은 실행할 포트입니다.
properties/launchSettings.json으로 이동하여 앱 이름을 찾고 이 아래에서 applicationUrl을 찾습니다. localhost:5000을 실행하고 있는 것을 볼 수 있습니다. 원하는 대로 변경하십시오. 그런 다음 dotnet run을 실행합니다... 만세.
다음을 사용하여 Netcore 3.1에서 포트 문제를 해결했습니다.
Program.cs 에서
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureWebHost(x => x.UseUrls("https://localhost:4000", "http://localhost:4001"))
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
다음을 사용하여 응용 프로그램에 액세스할 수 있습니다.
http://localhost:4000
https://localhost:4001
언급URL : https://stackoverflow.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted-on
'programing' 카테고리의 다른 글
| 소스= 호스트에 대한 ImageSourceConverter 오류 (0) | 2023.05.22 |
|---|---|
| 파일에서 임의의 줄 선택 (0) | 2023.05.22 |
| 비동기 함수를 호출하는 동안 Mocha 테스트에서 시간 초과를 방지하는 방법 오류: 시간 초과가 2000ms를 초과했습니다. (0) | 2023.05.22 |
| 'div'의 알려진 속성이 아니므로 'ngIf'에 바인딩할 수 없습니다. (0) | 2023.05.22 |
| sh와 bash의 차이 (0) | 2023.05.22 |