Angular 2和ASP.NET MVC 4路由无法一起使用 [英] Angular 2 and ASP.NET MVC 4 routing are not working together

查看:55
本文介绍了Angular 2和ASP.NET MVC 4路由无法一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ASP.NET MVC 4项目中实现了Angular2. MVC路由将覆盖Angular 2路由.如何同时处理两个路由?

I have implemented Angular2 in ASP.NET MVC 4 project. MVC routing is overriding Angular 2 routing. How can I handle both routing together?

我做了以下修改:

_layout.cshtml:

<base href="/"/>

app.routing.ts:

const route: Routes= [
{path: 'firstpage', component: firstComponent},
{path: 'secondpage', component: secondComponent}
]

我在RouteConfig.cs中的MVC默认路由上方添加了以下代码:

routes.MapRoute(
    name: "app1",
    url: "app1/{*url}",
    defaults: new { controller = "app1", action = "Index" }
    );

我尝试使用以下网址:

http://localhost:50450/app1/firstpage

http://localhost:50450/#/app1/firstpage

仍然无法正常工作,我该怎么办或者我错过了什么?

Still is not working, what should I do or did I miss something?

推荐答案

我处理它的方式是从RouteConfig中的角向路线添加相同的路线,如下所示:

the way I handled it is adding the same routes from the angular route in my RouteConfig like this:

        routes.MapRoute(
            name: "RouteName",
            url: "routeUrl",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

现在,如果路由为 http://localhost:3000/routeUrl ,.net端将将请求转发到主控制器,索引操作并将其呈现为类似于这样的请求 http://localhost:3000/然后,Angular 2路由器可以接管并完成其工作.

so now if the route is http://localhost:3000/routeUrl the .net side will just forward the request over to home controller, index action and render it as if it were a request like this http://localhost:3000/ then angular 2 router can take over and do its thing.

这篇关于Angular 2和ASP.NET MVC 4路由无法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆