MVC应用程序的路由我有3个MVC控制器:AngularJS与模板
- 仪表板(一种默认)
- 客户
- 员工 对于每一个,只有“指数”行动是可用只有“索引”视图。
在布局,我有3项一个简单的菜单:“仪表板”,“客户” 和“雇员”(@Html.ActionLink("Dashboard", "Index", "Dashboard")
)
没问题,我可以通过菜单中更改视图。
不,我添加了3个angularjs控制器(“/ App/Controllers/CustomerController”,...),这些控制器已被添加到一个包中。在布局页面中,有一个指向这个包的链接。
对于仪表板视图/Views/Dashboard/Index.cshtml
,我有这样的:
<div ng-controller="DashboardController" ng-cloak>
<div ng-view></div>
</div>
对于员工视图/Views/Employee/in Index.cshtml
,我有这样的:
<div ng-controller="EmployeeController" ng-cloak>
<div ng-view></div>
</div>
对于/Views/Customer/Index.cshtml
:
<ul class="nav navbar-nav">
<li><a href="#/Customer/List">List customers</a></li>
<li><a href="#/Customer/5">Detail</a></li>
</ul>
<div ng-controller="CustomerController" ng-cloak>
<!-- this is where content will be injected -->
<div ng-view></div>
</div>
当我启动应用程序(F5),我打了MVC的“索引” “仪表板”,但URL的样子:
http://localhost:2638/#/dashboard
我点击“雇员”的链接,我看到合适的内容,但该网址看起来像:
http://localhost:2638/Employee#/dashboard
我点击“客户”的链接,我看到合适的内容,但该网址的样子:
http://localhost:2638/Customer#/dashboard
我短声“客户名单”,我看到了模板的内容,但该网址看起来像:
http://localhost:2638/Customer#/Customer/List
我短声“详细信息”,我看到了模板的内容,但该网址看起来像:
http://localhost:2638/Customer#/Customer/5
所有内容都是正确的行为也是如此,网址是怪(即使我可以住在一起)。
我绑应用于:$locationProvider.html5Mode(true);
并删除我的菜单中的#但是在这种情况下, 时,点击“客户名单”,因为“/客户/列表”动作在MVC控制器缺少我得到一个错误。 我想只有索引文件。
当我在路由中删除,否则部分,从菜单中的URL“更好看”:
http://localhost:2638/Employee
在App.js文件I设置路由见下文。
我该如何解决这个问题?
路由:
myAppModule.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
.when('/Employee/List', {
templateUrl: 'App/Templates/Employee/List.html',
controller: 'MyEmployeeController',
caseInsensitiveMatch: true
})
.when('/Employee/:id?', {
templateUrl: 'App/Templates/Employee/Detail.html',
controller: 'MyEmployeeController',
caseInsensitiveMatch: true
})
.when('/Customer/List', {
templateUrl: 'App/Templates/Customer/List.html',
controller: 'MyCustomerController',
caseInsensitiveMatch: true
})
.when('/Customer/:id?', {
templateUrl: 'App/Templates/Customer/Detail.html',
controller: 'MyCustomerController',
caseInsensitiveMatch: true
})
.otherwise({
redirectTo: '/dashboard'
});
//$locationProvider.html5Mode(true);
}]);
你只能有一个'NG-应用= “ngApp”'在HTML中。例如,把它放在一个''或'
'。 – dfsq 2015-01-04 15:03:00已修复,但行为是一样的 – 2015-01-04 15:07:35
我会建议不要使用asp.net mvc,如果您使用角度路由,您可以非常容易地将节点与visual studio集成 – harishr 2015-01-04 15:28:35