【问题标题】:Angular routing html5mode ASP.NET角度路由 html5mode ASP.NET
【发布时间】:2014-11-03 04:15:38
【问题描述】:

我有一个基于 ASP.NET 项目的 angularjs 应用程序,它使用 html5mode 并且工作得非常好,除非我想传递路由参数。

角度路由:

 $locationProvider.html5Mode(true);

 $routeProvider.when('/accounts', {
            templateUrl: 'templates/accounts.html',
            controller: 'accountsCtrl',
            title: 'Accounts',
            resolve: {
                accounts: function (accountData) {
                    return accountData.getAll();
                }
            }
        });

        $routeProvider.when('/account/:accId', {
            templateUrl: 'templates/editAccount.html',
            controller: 'editAccountCtrl',
            title: 'Account',
            resolve: {
                account: function ($route, accountData) {
                    return accountData.get($route.current.pathParams.accId);
                }
            }
        });

web.config:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^index\.html" ignoreCase="false" />
          <action type="None" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="." ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />

        </rule>
      </rules>
    </rewrite>
  </system.webServer>

Rout localhost:3849/accounts - 有效,但是 localhost:3849/account/1 破坏了系统,看起来它无法加载 *.js 库或任何其他资源。

控制台显示

Uncaught SyntaxError: Unexpected token <

对于每个单独的库。怎么了? 谢谢

更新 问题是如何在 index.html 中引用脚本。 原文是:

<script type="text/javascript" src="Scripts/angular.min.js"></script>

必须是:

<script type="text/javascript" src="/Scripts/angular.min.js"></script>

所以当我要求服务器访问 localhost:3849/account/1 时,它开始查看 localhost:3849/account 中的所有文件,但没有找到并返回 index.html。 路由中同样的问题,而不是

templateUrl: 'templates/editAccount.html',

必须是:

templateUrl: '/templates/editAccount.html',

【问题讨论】:

    标签: javascript asp.net angularjs url-routing angular-routing


    【解决方案1】:

    试试这个(你可能需要调整 index.html 的路径):

    <?xml version="1.0" encoding="utf-8"?>
    
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=301880
      -->
    <configuration>
    
      <system.webServer>
      <rewrite>
        <rules>
          <rule name="AngularJS" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="../index.html" />
          </rule>
        </rules>
      </rewrite>
    
    </system.webServer>
    
    
    </configuration>
    

    【讨论】:

    • 嗯。该配置应该可以工作。我建议您使用 Fiddler 检查失败的请求。路由规则可能会返回默认文件 (html) 而不是您的资源。
    • 仔细观察,您的 URL 是如何引用的?如果 .js 文件没有正确植根,则相对路径会更改并导致您的 .js 文件为 404/重定向到 index.html。
    • 谢谢,我终于找到了问题——更新了问题。傻我=(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2016-03-09
    • 2015-06-13
    • 2017-11-08
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多