【发布时间】:2015-05-11 17:29:21
【问题描述】:
我们有一个现有/旧版 MVC 5 应用程序,并希望在主应用程序中“试验”一些 AngularJS SPA。
为了保持干净和独立,我们创建了一个新的 MVC 区域(应用程序已经使用了区域),并带有以下默认控制器...
namespace Acme.WebAdmin.Areas.Scheduler.Controllers
{
public class HomeController
: Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult PageOne()
{
return PartialView();
}
public ActionResult PageTwo()
{
return PartialView();
}
public ActionResult PageThree()
{
return PartialView();
}
}
}
SchedulerAreasRegistration.cs 文件如下所示:
public class SchedulerAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Scheduler";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Scheduler_default",
"Scheduler/{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
在 /Scheduler/Home/Index 视图中,我们有以下 HTML:
<div ng-app="SchedulerApp" ng-controller="ManagementPageController">
<p>{{models.helloAngular}}</p>
<ul>
<li><a href="/#/PageOne">PageOne</a></li>
<li><a href="/#/PageTwo">PageTwo</a></li>
<li><a href="/#/PageThree">PageThree</a></li>
</ul>
<div ng-view></div>
</div>
我们还为 AngularJS 应用程序添加了路由依赖:
var SchedulerApp = angular.module('SchedulerApp', ['ngRoute']);
SchedulerApp.controller('ManagementPageController', ManagementPageController);
var configFunction = function ($routeProvider, $locationProvider) {
$routeProvider.
when('/PageOne', {
templateUrl: 'Home/PageOne'
})
.when('/PageTwo', {
templateUrl: 'Home/PageTwo'
})
.when('/PageThree', {
templateUrl: 'Home/PageThree'
});
$locationProvider.html5Mode(true);
}
configFunction.$inject = ['$routeProvider', '$locationProvider'];
SchedulerApp.config(configFunction);
当我们运行应用程序并单击导航链接(例如“PageTwo”)时,我总是会返回到带有以下 URL http://localhost:52654/#/PageTwo 的标准网站主页。
我不确定问题是否是 AngualrJS 没有拦截引用,是否是 MVC 路由问题。
任何帮助/建议将不胜感激。
【问题讨论】:
-
试试templateUrl中的区域
Scheduler/Home/PageOne。 -
我试过了,但我仍然被重定向回“真实”主页,例如localhost:52654/#/PageOne - Angular 似乎没有拦截请求(我假设它就是这样做的),因为它实际上只是重定向到声明的 href='/#/PageOne'。我在页面上没有看到任何 JavaScript 错误。
-
您的 MVC 路由规则对于根级别和您所在的区域是什么样的?
-
我已将 SchedulerAreaRegistration.cs 内容添加到原始问题中 - 谢谢。
标签: asp.net-mvc angularjs