2014-11-03 115 views
2

我有一个基于ASP.NET项目的angularjs应用程序,它使用html5mode,并且完美地工作,除非我想要传递路由参数。Angular routing html5mode ASP.NET

角路由:

$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> 

溃败本地主机:3849 /账户 - 的作品,但本地主机:3849 /帐号/ 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 /帐号/ 1就开始寻找所有文件位于localhost:3849 /账户,没有找到他们并返回index.html。 路由同样的问题,而不是

templateUrl: 'templates/editAccount.html', 

,必须是:

templateUrl: '/templates/editAccount.html', 

回答

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> 
+0

无变化,对不起 – nelly2k 2014-11-03 04:32:03

+0

嗯。该配置应该可以工作。我建议你使用Fiddler来检查失败的请求。路由规则可能会返回默认文件(html)而不是您的资源。 – 2014-11-03 04:38:35

+0

更仔细地看,你的网址是如何引用的?相对路径会改变,并导致您的.js文件404 /重定向到index.html,如果他们没有正确的根。 – 2014-11-03 04:39:49