如何在HTML5模式下为URL重写AngularJS应用程序配置IIS?

我有 AngularJS seed project和我已经添加
$locationProvider.html5Mode(true).hashPrefix('!');

到app.js文件。我想配置IIS 7路由所有请求

http://localhost/app/index.html

所以这对我工作。我如何做到这一点?

更新:

我刚刚发现,下载并安装了IIS URL Rewrite module,希望这将使它容易和明显实现我的目标。

更新2:

我想这总结了我想实现(从AngularJS Developer documentation取得):

Using this mode requires URL rewriting on server side,
basically you have to rewrite all your links to entry point of your
application (e.g. index.html)

更新3:

我仍然在这工作,我意识到我需要不重定向(有规则,重写)某些URL,如

http://localhost/app/lib/angular/angular.js
http://localhost/app/partials/partial1.html

因此css,js,lib或partials目录中的任何内容都不会重定向。所有其他内容都需要重定向到app / index.html

任何人都知道如何轻松实现这一点,而无需为每个文件添加规则?

更新4:

我有2个入站规则在IIS URL Rewrite模块中定义。第一条规则是:

第二条规则是:

现在当我导航到localhost / app / view1它加载页面,然而支持文件(css,js,lib和partials目录中的那些)也被重写到app / index.html页面 – 所以一切都来了返回作为index.html页面,无论使用什么URL。我想这意味着我的第一条规则,这是应该防止这些URL被第二条规则处理,不工作..任何想法? …任何人? …我感觉很孤单… :-(

解决方法

我写在web.config一个规则$ locationProvider.html5Mode(true)在app.js中设置。

希望,帮助某人。

<system.webServer>
    <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>
  </system.webServer>

在我的index.html中,我将它添加到了< head>

<base href="/">

不要忘记在服务器上安装IIS URL Rewrite

此外,如果您使用Web API和IIS,这将工作,如果你的API在www.yourdomain.com/api,因为第三个输入(第三条线)。

相关文章

HTML5和CSS3实现3D展示商品信息的代码
利用HTML5中的Canvas绘制笑脸的代码
Html5剪切板功能的实现
如何通过HTML5触摸事件实现移动端简易进度条
Html5移动端获奖无缝滚动动画实现
关于HTML5和CSS3实现机器猫的代码