首页 > 解决方案 > angularjs 默认页面,无需键入 index.html

问题描述

我正在使用 VS 2017 社区创建一个 angularjs 应用程序。当我在本地运行应用程序时,我会在地址栏中获得带有端口的 localhost,但默认情况下它不会带我进入 index.html。如果我在其中键入 index.html ,那么它可以工作,但是由于所有路由都通过连接在 index.html 中的 angular 应用程序,这会破坏路由。我无法输入 localhost:portnumber/my_other_route_here,因为它不会通过 index.html 中定义的 angularjs 应用程序。如何让 IISExpress 自动路由到 index.html 而无需键入 index.html?

我的配置文件中有以下内容,并认为它会对此有所帮助,并且过去有,所以不确定这次有什么不同。

<!-- Must have this so that direct entries in the browsers address bar get routed correctly. This routes entires that don't have physical paths to index.htm which
    will route it correctly if a path is setup. Basically this allows for pretty url's like localhost:1234/red -->
    <rewrite>
      <rules>
        <rule name="angularjs routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
    <!-- END rules -->

标签: angularjsvisual-studio-2017

解决方案


所以我为 Web API(不是 MVC)使用了默认的 ASP.NET 应用程序模板,但它还是添加了 MVC。它有一个正在做一些映射的 RegisterRoutes() 的事实似乎就是发生这种情况的原因。当我在这里注释掉所有内容(可能会删除整个文件)时,默认情况下它会打开 index.html 而无需将其放在地址栏中。


推荐阅读