AngularJS在IIS和<default document>上使用HTML5mode(true)

Bra*_*tin 4 .net iis html5 web angularjs

我有一个在IIS 8.0(arvixe托管)上运行的简单应用程序,它使用AngularJS作为SPA框架.我打开html5mode并将标签链接到index.html(根文件),我还将此文件设置为web.config中的文件.我知道你必须做我所拥有的服务器端重写.

我想要实现的目标如下:

用户输入"domain.com",该网站会加载domain.com/home.当然在URL下实际上是domain.com/index.html/home但是html5mode用重写来处理它.

这是app.config,这很好.

  .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider ) {
$routeProvider
    .when('/home', {
        templateUrl: 'templates/home.html',
        controller: 'HomeController'
    })
    .when('/login', {
        templateUrl: 'templates/login.html',
        controller: 'LoginController'
    })
    .when('/forum', {
        templateUrl: 'templates/forum.html',
        controller: 'ForumController'
    })
    .when('/rebellinks', {
        templateUrl: 'templates/rebellinks.html',
        controller: 'RebelLinksController'
    })
    .when('/events', {
        templateUrl: 'templates/events.html',
        controller: 'EventsController'
    })
    .when('/oldnews', {
        templateUrl: 'templates/oldnews.html',
        controller: 'OldNewsController'
    })
    .otherwise({
        redirectTo: '/home'
    });

//use HTML5 History API
$locationProvider.html5Mode(true);
   }]);
Run Code Online (Sandbox Code Playgroud)

IIS的Web.Config文件:

 <system.webServer>
    <rewrite>
      <rules>
        <clear />       
        <!--<rule name="jsRule" stopProcessing="true">
          <match url=".js" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" />
          </conditions>
          <action type="None" />
        </rule>-->

        <rule name="SRBShome" enabled="true" stopProcessing="true">
          <match url="^(.*)$" />
          <action type="Rewrite" url="/index.html" />
        </rule>
        <rule name="AngularJS" enabled="false" 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="/index.html" />
        </rule>        
      </rules>
    </rewrite>
    <defaultDocument enabled="true">
      <files>
        <clear />
        <add value="index.html" />
        <add value="home.html" />
      </files>
    </defaultDocument>
Run Code Online (Sandbox Code Playgroud)

这样做是在用户访问网站时加载domain.com/home但是.js文件被转移,因为我在重写中添加了一个特殊规则以防止它们被重写但我认为我的SRBShome规则上的模式需要调整工作正常.

任何帮助赞赏.

Bra*_*tin 14

通过更改下面的重写规则解决了这个问题.这允许在IIS上加载index.html.由于某些原因,web.config不适用于此设置.所以现在当用户请求domain.com时,重写通过'/index.html',然后AngularJS'html5Mode启动并清理URL并删除hashbang.

 <rewrite>
      <rules>
        <clear />        
        <rule name="SRBShome" enabled="true" stopProcessing="true">
          <match url="^(.+)$" negate="true" />
          <conditions>
            <add input="{REQUEST_URL}" pattern="^(.+)$" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" logRewrittenUrl="true"/>
          <!--<match url="^.com" />
          <action type="Rewrite" url="/index.html" />-->
        </rule>
        <rule name="AngularJS" enabled="true" 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="/index.html" />
        </rule>        
      </rules>
    </rewrite>
Run Code Online (Sandbox Code Playgroud)