在IIS Express中使用自定义域

在IIS Express中使用自定义域,iis,iis-7,iis-express,Iis,Iis 7,Iis Express,传统上,我在本地主机开发服务器上使用自定义域。大致如下: dev.example.com dev.api.example.com 这为我在使用外部API(如Facebook)时提供了极大的灵活性。这在过去的内置VisualStudio开发服务器中非常有效,因为我所需要做的就是向指向127.0.0.1的DNS记录添加一个CNAME 但是,我无法将其用于IIS Express。我尝试过的一切似乎都失败了。我甚至为iisexpress的applicationHost.config文件添加了正确的XM

传统上,我在本地主机开发服务器上使用自定义域。大致如下:

dev.example.com
dev.api.example.com
这为我在使用外部API(如Facebook)时提供了极大的灵活性。这在过去的内置VisualStudio开发服务器中非常有效,因为我所需要做的就是向指向
127.0.0.1
的DNS记录添加一个CNAME

但是,我无法将其用于IIS Express。我尝试过的一切似乎都失败了。我甚至为iisexpress的
applicationHost.config
文件添加了正确的XML配置,但它似乎没有像真正安装IIS那样识别条目的有效性

<binding protocol="http" bindingInformation="*:1288:dev.example.com" />

每当我输入此行并尝试请求
http://dev.example.com:1288
我收到以下信息:

错误的请求-无效的主机名


有人知道我是否遗漏了一些明显的东西吗?或者IIS Express团队是否真的缺乏预见性,无法看到这种类型的使用?

无效的主机名表示您在IIS Express配置文件中配置的实际站点(很可能)没有运行。IIS Express不像IIS那样具有进程模型


要运行您的站点,需要显式启动它(通过打开webmatrix并从webmatrix访问,或通过命令行调用iisexpress.exe(从它的安装目录)并使用/site参数启动)


通常,允许完全限定的DNS名称用于本地访问的步骤如下 让我们使用DNS名称dev.example.com的示例

  • 编辑%windows%\system32\drivers\etc\hosts文件,将dev.example.com映射到127.0.0.1(需要管理员权限)。如果您控制DNS服务器(如Nick的情况),则DNS条目就足够了,因为不需要此步骤
  • 如果您通过代理访问internet,请确保dev.example.com不会被禁止代理(您必须在浏览器的例外列表中输入(例如,它将是工具/internet选项/连接/Lan设置),然后转到代理服务器/高级,并将dev.example.com放在例外列表中)
  • 为您的站点(例如:Site1)配置IIS Express绑定以包括dev.example.com。使用该绑定需要管理权限。或者,可以使用http.sys进行一次性URL保留

    netsh http添加urlacl url=http://dev.example.com:/ 用户=

  • 启动
    iisexpress/site:Site1
    或在WebMatrix中打开Site1


  • 按照Jaro的建议,我可以在Windows XP和IIS Express(通过Web Matrix安装)下使用它,只需稍加修改,而不仅仅限于本地主机。这只是正确设置绑定的问题

  • 使用WebMatrix从web应用程序根目录中的文件夹创建新网站
  • 关闭WebMatrix
  • 打开
    %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
    (Windows XP.Vista和7个路径将类似)并编辑
    配置块中的站点定义,使其符合以下要求:

    
    

  • 如果运行MVC,则将应用程序池设置为“集成”选项之一。

    这对我来说是有效的(针对VS 2013更新,请参阅2010年修订历史,对于VS 2015,请参阅此:):

  • 右键单击Web应用程序项目▶ <代码>属性▶ <代码>网络,然后按如下方式配置
    服务器
    部分:

    • 选择IIS Express▼ 从下拉列表中
    • 项目Url:
      http://localhost
    • 覆盖应用程序根URL:
      http://dev.example.com
    • 单击创建虚拟目录(如果此处出现错误,您可能需要禁用IIS 5/6/7/8,将IIS的
      默认站点更改为除端口
      :80
      、确保等以外的任何内容。)
  • 可选:将
    开始URL
    设置为
    http://dev.example.com

  • 打开
    %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
    (Windows XP、Vista和7)并编辑
    配置块中的站点定义,使其符合以下要求:

    <site name="DevExample" id="997005936">
        <application path="/" applicationPool="Clr2IntegratedAppPool">
            <virtualDirectory
                path="/"
                physicalPath="C:\path\to\application\root" />
        </application>
        <bindings>
            <binding
                protocol="http"
                bindingInformation=":80:dev.example.com" />
        </bindings>
        <applicationDefaults applicationPool="Clr2IntegratedAppPool" />
    </site>
    
    
    并添加行
    127.0.0.1 dev.example.com

  • ► 启动您的应用程序

  • 从评论中可以得到一些很好的建议:

    • 您可能需要以管理员身份运行Visual Studio
    • 如果您想让其他开发人员看到您的IIS运行
      netshhttp添加urlacl url=http://dev.example.com:80/ 用户=所有人
    • 如果要为所有主机解析站点,请设置
      bindingInformation=“*:80:”
      。 使用您想要的任何端口,80都很方便。要解析所有主机,您需要以管理员身份运行Visual Studio

    在我的WebMatrix IIS Express安装中,从
    “*:80:localhost”
    更改为
    “*:80:custom.hostname”
    不起作用(“坏主机名”,即使使用正确的etc\hosts映射),但是
    ”*:80:“
    确实起作用了,并且没有其他答案所需的任何附加步骤。请注意
    “*:80:*”
    不行;请去掉第二个星号。

    我试图将公共IP地址集成到我的工作流程中,但这些答案没有帮助(我喜欢使用IDE作为IDE)。但是上面的问题让我找到了解决方案 (我花了大约2个小时的时间把头撞在墙上,让它与Visual Studio 2012/Windows 8集成)以下是我最终的工作成果

    applicationhost.config由VisualStudio在
    C:\Users\usr\Documents\IISExpress\config下生成

        <site name="MySite" id="1">
            <application path="/" applicationPool="Clr4IntegratedAppPool">
                <virtualDirectory path="/" physicalPath="C:\Users\usr\Documents\Visual Studio 2012\Projects\MySite" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:8081:localhost" />
                <binding protocol="http" bindingInformation="*:8082:localhost" />
                <binding protocol="http" bindingInformation="*:8083:192.168.2.102" />
            </bindings>
        </site>
    
    
    
    • 将IISExpress设置为以管理员身份运行,以便它可以绑定到外部
          <site name="MySite" id="1">
              <application path="/" applicationPool="Clr4IntegratedAppPool">
                  <virtualDirectory path="/" physicalPath="C:\Users\usr\Documents\Visual Studio 2012\Projects\MySite" />
              </application>
              <bindings>
                  <binding protocol="http" bindingInformation="*:8081:localhost" />
                  <binding protocol="http" bindingInformation="*:8082:localhost" />
                  <binding protocol="http" bindingInformation="*:8083:192.168.2.102" />
              </bindings>
          </site>
      
      netsh http add urlacl url="http://*:portnumber/" user=everyone
      netsh http add urlacl url="http://localhost:portnumber/" user=everyone
      
      <site name="MvcApplication1" id="2">
          <application path="/" applicationPool="Clr4IntegratedAppPool">
              <virtualDirectory path="/" physicalPath="C:\sites\MvcApplication1" />
          </application>
          <bindings>
              <binding protocol="http" bindingInformation="*:12853:localhost" />
              <binding protocol="http" bindingInformation="*:12853:*" />
          </bindings>
      </site>
      
      netsh http add urlacl url=http://yourcustomdomain.com:80/ user=everyone 
      
      "C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /+bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
      
      netsh http delete urlacl url=http://yourcustomdomain.com:80/
      
      "C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /-bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
      
      \.vs\config
      
      127.0.0.1       jam.net
      127.0.0.1       www.jam.net
      
      <site name="JBN.Site" id="2">
          <application path="/" applicationPool="Clr4IntegratedAppPool">
              <virtualDirectory path="/" physicalPath="C:\Dev\Jam\shoppingcart\src\Web\JBN.Site" />
          </application>
          <bindings>
              <binding protocol="http" bindingInformation="*:49707:" />
                  <binding protocol="http" bindingInformation="*:49707:localhost" /> 
          </bindings>
      </site>
      
      
      ::The following configures IIS Express to bind to any address at the specified port
      
      ::remove binding if it already exists
      "%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /-bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"
      
      ::add the binding
      "%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /+bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"
      
      
      <bindings> 
          <binding protocol="http" bindingInformation="*:12345:localhost" />
          <binding protocol="http" bindingInformation="*:12345:whatever.localhost" />
      </bindings>
      
      127.0.0.1 dev.example.com
      ::1 dev.example.com
      
      <bindings>
          <binding protocol="http" bindingInformation="*:5737:localhost" />
          <binding protocol="https" bindingInformation="*:44320:*" />
      </bindings>
      
      127.0.0.1 customdomain