由于路线根据顺序而优先,所以您只需要明确您的路线在您的MVC路线中的顺序。例如,它看起来像
/app/Facturacion/
是您希望Angular运行的位置。因此,在你的MVC路线,在RouteConfig
routes.MapRoute(
name: "ngRoute",
url: "app/Facturacion/{*angularRoute}",
defaults: new {controller = "Facturacion", action = "Index" }
);
所以现在添加为第一条路线,任何时候你打的路线开始应用/ Facturacion /,棱角分明的路线将接管。否则它将跳过并继续使用MVC路由。如果有一个特定的MVC路径使用与angular(/ app/Facturacion /)相同的路径,请将其添加到RouteConfig中的ngRoute上方,这将继续运行MVC而不是角度。
routes.MapRoute(
name: "myMvcRoute",
url: "app/Facturacion/someRoute",
defaults: new {controller = "Facturacion", action = "SomeAction" }
);
确保在您的FacturacionController该指数操作返回查看,并在您查看,添加您的角度脚本和角码。视图的例子会是这样的
@{
ViewBag.Title = "Facturacion";
}
@section head{
<base href="/"> //Add a head section in your layout (make it optional)
}
@section scripts{
@Scripts.Render("~/bundles/angularjs")
@Scripts.Render("~/bundles/facturacionSPA")
}
<div ng-app="facturacionApp">
<div ng-view></div>
</div>
你角路线看起来像
$routeProvider.when('/app/Facturacion/MantenimientoFacturas', {
templateUrl: '/Facturacion/MantenimientoFacturas',
controller: 'FacturasController'
}).when('/app/Facturacion/MantenimientoOrdenes', {
templateUrl: '/Facturacion/MantenimientoOrdenes',
controller: 'OrderController'
}).otherwise({
redirectTo: '/ServiceDesk/starter',
});
此外,角模板应该是普通的HTML文件,而不是局部视图。更好地坚持角的东西和MVC的mvc东西角,不要混在我看来!
编辑: 如果“/的ServiceDesk /起动器”是一个MVC的路线,你不想角路由到它,你可以做这样的事情在你的角度控制器:
if ($location.url().indexOf('/ServiceDesk/starter') >= 0 && supports_history_api()) $window.location.href = '/ServiceDesk/starter';
代码supports_history_api()为完整性,这决定了我是否使用$ locationProvider的html5模式,因为我需要旧的IE来工作
function supports_history_api() {
return !!(window.history && history.pushState);
}
谢谢你的回复!我有这个疑问,如果我映射此路线:routes.MapRoute( name:“myMvcRoute”, url:“app/Facturacion/someRoute”, 默认值:新{controller =“Facturacion”,action =“SomeAction”} );在MVC中,但不是在$ routeProvider中,应用程序从未打过网址;它只会将我重定向到.otherwise,即使我删除。其他方式,它会重定向到基础href,它看起来像我强制添加$ routeProvider上的每条路线,我是吗? – user3542595
是的,角度的应用程序永远不会打这个网址,我说如果你想运行一个正常的MVC网址,而不是运行角度的应用程序。但是我想从angular角度加载这个url来退出mvc,使用$ window.location.href,不要使用角度$ location(我总是这样做)。所以要从角度到mvc,使用$ window.location.href = yourMVCroute。要保持角度,请使用$ location。 – garethb
请参阅编辑我的答案 – garethb