2016-03-21 18 views
0

Ich habe Winkel 2 ts app, wobei der Router wie folgt definiert:Angular 2 Routing/Navigation

@RouteConfig([ 
     { path: '/accounts', name: 'Accounts', component: AccountComponent, useAsDefault: true}, 
     { path: '/transactions', name: 'Transactions', component: TransactionComponent} 
]) 

Wenn ich geben Sie in Browser: http://localhost/ die URL-Updates mit http://localhost/accounts und funktioniert wie erwartet, aber wenn ich http://localhost/accounts es eingeben http://localhost/accounts/accounts werden und fehlschlagen.

Was ist los?

Das Backend -ASP.NET 5, die index.html beziehen, ist für alle unbekannte Wege

app.Use(async (context, next) => 
     { 
      await next(); 

      if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) 
      { 
       context.Request.Path = "/index.html"; 
       await next(); 
      } 
     }); 

der Startabschnitt: nur beachteten

public void Configure(IApplicationBuilder app) 
    { 
     app.UseIISPlatformHandler(); 

     app.Use(async (context, next) => 
     { 
      await next(); 

      if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) 
      { 
       context.Request.Path = "/index.html"; 
       await next(); 
      } 
     }); 

     app.UseDefaultFiles(); 
     app.UseStaticFiles(); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute("spa-fallback", "{*anything}", new {controller = "Home", action = "Index"}); 
     }); 

    } 
+0

Versuchen Hinzufügen ' 'in in der Datei index.html. –

+0

hatte es schon, so Problem woanders – nelly2k

Antwort

1

Aktualisiert

I Die useAsDefault-Option in Ihrer eckigen Router-Konfiguration (Ich habe vorher noch nicht angular 2 verwendet). Ich nehme an, dass automatisch die/accounts zu Ihrer URL hinzugefügt werden. Versuchen Sie, diese Config

public void Configure(IApplicationBuilder app) 
{ 
    app.UseIISPlatformHandler(); 

    app.Use(async (context, next) => 
    { 
     await next(); 

     if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) 
     { 
      ctx.Response.Redirect("/"); 
      return; 
     } 
    }); 

    app.UseDefaultFiles(); 
    app.UseStaticFiles(); 

} 

Was passiert, wenn Sie die Route direkt in den Browser eingeben, wird es durch die asp.net Routing-Engine erwischt zu werden. Was Sie tun müssen, ist, dass die Laufzeit diese Route abfängt und nicht behandelt.

Was jetzt passieren soll, ist, dass Ihre Startseite vom Server gerendert wird und alle zusätzlichen JS auf der Route ausgeführt werden.

+0

Nun, habe ich mir diesen Code Start und es gibt keine Änderungen, das Problem besteht immer noch – nelly2k

+0

könnten Sie den Abschnitt Ihres Start-up, die Ihre Routen und andere Middleware definiert? – cecilphillip

+0

in der Frage – nelly2k