You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置IIS将ASP.NET Core端点部署到自定义域名?

问题描述

测试Demo代码如下:

控制器代码

public class CrotolamoController : ControllerBase
{
    [HttpPost]
    [Route("/")]
    public async Task<String> test()
    {
        return "hello world";
    }
}

launchSettings.json配置

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "https://www.crotolamo.com",
      "sslPort": 443
    }
  },
  "profiles": {
    "Test": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://www.crotolamo.com",
      "sslPort": 443,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

该Demo在Visual Studio中运行正常,但用Postman发送POST请求时出现错误:

Error: getaddrinfo ENOTFOUND www.crotolamo.com

在Windows Server 2019的IIS上部署时,使用以下绑定配置仍出现相同错误:

  • 类型:https
  • IP地址:全部未分配
  • 端口:443
  • 主机名:www.crotolamo.com
  • SSL证书:IIS Express开发证书

仅使用localhost时可正常运行,已尝试在Windows 10开发机的hosts文件添加127.0.0.1 www.crotolamo.com但无效,请问如何修改配置以使用自定义域名替代localhost?


解决方法

1. 确保Hosts文件配置生效

  • 用管理员权限打开记事本,路径:C:\Windows\System32\drivers\etc\hosts
  • 添加或确认以下两行内容:
    127.0.0.1 www.crotolamo.com
    ::1 www.crotolamo.com
    
  • 保存后,打开管理员命令提示符执行ipconfig /flushdns刷新DNS缓存
  • 验证配置:执行ping www.crotolamo.com,如果返回127.0.0.1的响应,说明Hosts配置生效

2. 调整ASP.NET Core应用的URL绑定

开发环境(Visual Studio)

  • 修改launchSettings.json中的applicationUrl,同时包含localhost和自定义域名:
    "applicationUrl": "https://localhost:443;https://www.crotolamo.com:443"
    

IIS部署环境

  • 如果是本地测试服务器,需在Windows Server 2019的Hosts文件同样添加127.0.0.1 www.crotolamo.com;如果是远程服务器,要确保DNS解析指向服务器公网IP
  • 更换匹配域名的SSL证书:IIS Express开发证书仅适配localhost,需生成对应域名的自签名证书(测试用):
    打开管理员PowerShell执行:
    New-SelfSignedCertificate -DnsName "www.crotolamo.com" -CertStoreLocation "Cert:\LocalMachine\My"
    
    生成后,在IIS站点绑定中选择该新证书

3. 处理端口与权限问题

  • 检查443端口占用:打开命令提示符执行netstat -ano | findstr ":443",若有其他进程占用,需停止该进程或更换端口
  • 给应用分配端口监听权限:打开管理员命令提示符执行:
    netsh http add urlacl url=https://www.crotolamo.com:443/ user=Everyone
    

4. Postman请求验证

  • 确认请求URL为https://www.crotolamo.com,无拼写错误
  • 若使用自签名证书,在Postman设置中关闭SSL certificate verification(仅测试环境使用)

内容的提问来源于stack exchange,提问作者Felipe La Rotta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:01:24