Visual StudioでASP.NET Coreの開発をする際、開発マシンでの検証だけでなく、iPhoneやAndroidでの表示を確認したい時があります。
標準だと localhost
で指定されており、そのままでは外部からアクセスはできないため、設定する必要があるのでメモ。
前提
ファイアーウォールで該当ポートが解放されていること。
launchSettings.jsonでの指定
launchSettings.json
のapplicationUrlは初期設定ではlocalhostになっているので、ここを開発マシンのプライベートIPアドレスに変更。
この方法、なぜかlaunchBrowser
をtrueにしてるのに起動しない謎。
appsettings.jsonでの指定
こちらのほうがおすすめ。
appsettings.json
またはappsettings.Development.json
に urls
で http://*:5000;https://*:5001
を指定。
こちらだとホスト名がlocalhostだろうが開発マシンのIPアドレスだろうが行けるので便利。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"urls": "http://*:5000;https://*:5001", | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
} | |
} |
そのほか
CreateHostBuilder
で UseUrls
に指定する方法があるんですが、ソースコードに書くのはちょっと・・・。