Angular 2 and ASP.NET MVC 4 routing are not working together

亡梦爱人 提交于 2020-01-17 03:19:25

问题


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

I have done following modification:

_layout.cshtml:

<base href="/"/>

app.routing.ts:

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

I have added below code above MVC default route in RouteConfig.cs:

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

I have tried using below urls:

http://localhost:50450/app1/firstpage

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

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


回答1:


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 }
        );

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.



来源:https://stackoverflow.com/questions/40831923/angular-2-and-asp-net-mvc-4-routing-are-not-working-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!