1

I am creating an application in Asp.Net-MVC in VS2015. I want that application should support MVC routing and Angular 2 routing both.

I need MVC routing for Menu bar and Angular 2 routing for wizard present in each page lie below image.

Wizard

I have gone through various articles and finally used below code in my RouteConfig.cs file:

 routes.MapRoute(
            name: "spa-fallback",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index" }
            );

But this is not working properly. When i go from Home/Index to Home/Contact, it should not consider the angular 2 routing . But it is considering the angular 2 routing and showing the error :

Error: Cannot match any routes: 'Home/Contact'(…)

Below is screenshot:

Erros

How could i made the application compatible with both routings. Thanks

Sometimes Code
  • 591
  • 3
  • 14
  • 33
  • How do you redirect to this route? Click a url, type in the browser, etc.? – Ilya Chernomordik Nov 08 '16 at 07:28
  • I have an anchor tag .. Contact on my index page...Page is redirecting to the Contact page but the console have an error when the contact page render.. – Sometimes Code Nov 08 '16 at 08:37
  • I think you need to use the full URL like http://..../Home/Contact – Ilya Chernomordik Nov 08 '16 at 08:39
  • Alternatively check here: it's for Angular 1, but it might do the trick in Angular2 as well: http://stackoverflow.com/questions/16002984/angularjs-how-can-i-do-a-redirect-with-a-full-page-load. The problem you have is that angular2 interprets the a tag and override it. You need to prevent angular from doing it. – Ilya Chernomordik Nov 08 '16 at 08:40

1 Answers1

2

There could be a workaround like already described in the answer to this question: How to use ASP.NET MVC and AngularJS routing?

So, in short, define a route that would catch urls intended for mvc controllers for sure, i.e. add a prefix

routes.MapRoute(
        name: "spa-fallback",
        url: "app/{*url}",
        defaults: new { controller = "Home", action = "Index" }
);
Community
  • 1
  • 1
NamiraJV
  • 658
  • 1
  • 7
  • 11