I'm trying to redirect from an MVC controller to an Angular 2 app that resides within an area of the MVC project, structure is as follows:
Clients.Web (MVC project)
|
+--Areas
|
+--Legacy
| |
| +--Controllers
| |
| +--Action1ApiController
|
+--Web
|
+--app
| |
| +--action1
| | |
| | +--action1.component.ts
| |
| +--action2
| | |
| | +--action2.component.ts
| |
| +--app.component.ts
| +--index.html
| +--main.ts
|
+--assets (vendor modules, images etc.)
+--Controllers
| |
| +--AppController.cs
|
+--node_modules
+--typings
|
...
I need to be able to traverse from a legacy MVC page into the Angular 2 app, e.g.
/Web/App/Action1?param1=10¶m2=15
should be routed to
/Action1/:param1/:param2
in Angular. At the same time I need to be able to invoke actions on
/Areas/Legacy/Controllers/Action1ApiController
I can't, for the love of cookies, figure out how to do that. In my index.html I set the base href to
<base href="/Areas/Web/">
In that same file I imported the main module with SystemJS,
<script>
System.config({
baseURL: './',
map: {
'angular2': '/Areas/Web/node_modules/angular2',
'rxjs': '/Areas/Web/node_modules/rxjs'
}
});
System.defaultJSExtensions=true;
System.import('/Areas/Web/app/main')
.then(null,console.error.bind(console));
</script>
where I then bootstrap the app
import { bootstrap } from 'angular2/platform/browser';
import { AppComponent } from './app.component';
bootstrap(AppComponent, [])
.then(success => console.log('Bootstrap success'))
.catch(error => console.log(error));
Finally, in app.component I set up routing
import { Component } from 'angular2/core';
import { ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, Route, Redirect } from 'angular2/router';
import { Component1} from './action1/action1.component';
import { Component2} from './action2/action2.component';
@Component({
selector: "app",
templateUrl: 'app/app.component.html',
styleUrls: ['app/app.component.css'],
directives: [
ROUTER_DIRECTIVES,
Action1Component,
Action2Component
],
providers: [
ROUTER_PROVIDERS
]
})
@RouteConfig([
new Route({ path: '/:param1/:param2, name: 'Action1', component: Action1Component})
])
export class AppComponent {
}
However, if I then navigate to
/Areas/Web/app/index.html?param1=10¶m2=!5
I get
param1 = 'app'
param2 = 'index.html'
passed into Action1Component, which is clearly not what I intended. Any pointers would be appreciated.
EDIT:
Routing only targets the AppController at the moment,
routes.MapRoute("SPA", "Web/{controller}/{action}/{id}",
new { controller = "App", action = "Index", area = "Web", id = UrlParameter.Optional },
new[] { "SomeSite.Areas.Web.Controllers" });
and the code for the action is
public class AppController : Controller
{
public ActionResult Index(int param1, int param2)
{
return Redirect($"/Areas/Web/app/index.html?param1={param1}¶m2={param2}");
}
}
As I'm migrating a legacy web site one page at a time, the idea is to have actions Action1, Action2 etc. on the AppController and link to these actions from legacy pages.