.net - 在 IIS 和 <default document> 上使用 HTML5mode(true) 的 AngularJS

标签 .net angularjs html iis web

我有一个在 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);
   }]);

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>

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

感谢任何帮助。

最佳答案

通过更改下面的重写规则解决了这个问题。这允许在 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>

关于.net - 在 IIS 和 <default document> 上使用 HTML5mode(true) 的 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749776/

相关文章:

c# - Equals 方法的奇怪行为

未提供的 C# 数据库插入 : The parameterized query expects the parameter,

html - 在 select 标签中使用 ngRepeat 两次

java - 阅读网站,就像用户使用 HttpComponents 看到的一样

c# - ASP.net MVC - 每个 View 或每个操作一个 ViewModel?

c# - 分组 2 个表,计算值,然后将结果保存在字典中

javascript - Controller AngularJs 中的服务注入(inject)

javascript - 多个指令要求模板

html - 在 div 内对齐 2 个跨度,一个向左,另一个向右

php - 创建一个 PHP 随机段落生成器