禁用ASP.NET Core开发环境的HTTPS
目录
在开发ASP.NET Core的项目时,默认地是使用HTTPS安全协议的。有时候可能不希望在本地的开发环境中使用HTTPS,更改这一默认行为非常简单。
移除
UseHttpsRedirection
中间件app.UseHttpsRedirection
中间件会将所有HTTP请求重定向到HTTPS,因此首先我们需要将其删除。中间件配置一般在Program.cs或Startup.cs中。配置launchSettings.json
launchSettings.json在项目的Properties目录下,它只对本地的开发环境生效,部署时会被忽略。通过
dotnet new
或者Visual Studio生成的ASP.NET Core项目会创建launchSettings.json文件。
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:16717",
"sslPort": 44324
}
},
"profiles": {
"WebApplication1": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7072;http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
将applicationUrl
从https更改为http即可更改默认的应用启动Url,若使用IIS启动,还需将iisSettings
中的sslPort
设为0。
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:16717",
"sslPort": 0
}
},
"profiles": {
"WebApplication1": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
此外,在launchSettings.json中,也可以通过applicationUrl
更改应用启动的端口号。
关注微信公众号“捕获异常”,获取最新文章推送,提升你的技能。