2017-09-28 8 views
0

.NETコアの静的ファイルでアプリケーションを処理すると、ブラウザのURLを変更できません。そうすると、私は与えられた.NET Core、Angular2、NancyFX - 404ブラウザでURLが変更される

404 - Not Found

ナンシーエラーページ。

動作している唯一のURLは、ベースURL localhost:5000です。 localhost:5000/reservationsにURLを変更すると404が表示されます。ベースURLにない場合はF5を押すと404になります。

localhost:4200のbrowsersync URLを使用するとすべてが機能し、 404を使わずにF5キーを押します。これは開発時に行っていることですが、IISを使用してサーバーに展開されているときは実行されません。 IIS Expressのアプリケーション

  1. NGビルド(ファイルはindex.htmlを一緒にwwwrootのためにコピーされます)

  2. 開始IIS Expressの

を実行

Startup.cs

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
      .AddEnvironmentVariables(); 
     Configuration = builder.Build(); 

     env.ConfigureNLog("nlog.config"); 
    } 

    public IConfiguration Configuration { get; } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddAuthentication(IISDefaults.AuthenticationScheme); 

     services.AddMvc(config => 
     { 
      var policy = new AuthorizationPolicyBuilder() 
       .RequireAuthenticatedUser() 
       .Build(); 
      config.Filters.Add(new AuthorizeFilter(policy)); 
     }); 

     services.AddMvc(); 

     services.InjectServices(); 

     services.AddOptions(); 

     services.Configure<Endpoints>(Configuration.GetSection("Endpoints")); 

     services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 

     services.AddSingleton<IConfiguration>(Configuration); 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     app.Use(async (context, next) => 
     { 
      await next(); 
      if (!context.Request.Path.Value.StartsWith("/api/")) 
      { 
       context.Request.Path = "/index.html"; 
       await next(); 
      } 
     }); 

     app.UseDefaultFiles(); 

     app.UseStaticFiles(); 

     loggerFactory.AddNLog(); 

     app.AddNLogWeb(); 

     app.UseOwin(x => x.UseNancy(new NancyOptions 
     { 
      Bootstrapper = new Bootstrapper(app) 
     })); 
    } 
} 

Bootstrapper.cs

public class Bootstrapper : DefaultNancyBootstrapper 
{ 
    readonly IApplicationBuilder _app; 

    public Bootstrapper(IApplicationBuilder app) 
    { 
     _app = app; 
    } 

    protected override void ConfigureApplicationContainer(TinyIoCContainer container) 
    { 
     base.ConfigureApplicationContainer(container); 

     container.Register<IOptions<Endpoints>>(_app.ApplicationServices.GetService<IOptions<Endpoints>>()); 

     container.Register<IReservationService>(_app.ApplicationServices.GetService<IReservationService>()); 
    } 
} 

wwwrootの

enter image description here

index.htmlを

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>XXX</title> 
    <base href="/"> 
    <meta name="viewport" content="width=device-width,initial-scale=1"> 
    <link rel="icon" type="image/x-icon" href="XXX"> 
    <link href="styles.61cc257ef8131303860b.bundle.css" rel="stylesheet" /> 
</head> 
<body> 
    <app-root></app-root> 
    <script type="text/javascript" src="inline.f68c652d205feae6e02a.bundle.js"></script> 
    <script type="text/javascript" src="polyfills.756a44edc7e0a68ce65c.bundle.js"></script> 
    <script type="text/javascript" src="vendor.940a3ce39a746f717f7b.bundle.js"></script> 
    <script type="text/javascript" src="main.d5f9dd82a3600822cae2.bundle.js"></script> 
</body> 
</html> 

私は、サーバーからの静的コンテンツの取り扱いには問題があることを理解していますが、わかりません。

.NET CoreでAngularアプリケーションをホストするときに、ブラウザURLを使用して移動するにはどうすればよいですか?

アップデート1:

追加した後:

PerformPassThrough = (context => context.Response.StatusCode == HttpStatusCode.NotFound) 

404エラーメッセージが消えるが、ページには、コンソールで404以外のすべてのエラーメッセージなしで今のブランク..です

enter image description here

答えて

0
  1. bootstrapper.csにルートパスを設定しました。

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    //For serving index.html on baseurl - this can we done in AppNancyModule aswell: Get("/", _ => View["index"]); 
    app.UseDefaultFiles(); 

    app.UseStaticFiles(); 

    loggerFactory.AddNLog(); 

    app.AddNLogWeb(); 

    app.UseOwin(x => x.UseNancy(new NancyOptions 
    { 
     Bootstrapper = new Bootstrapper(app, env) 
    })); 
} 

AppNancyModule

public class AppNancyModule : NancyModule 
{ 
    public AppNancyModule() 
    { 
     //Get("/", _ => View["index"]); 

     Get(@"^(.*)$", _ => View["index"]); 
    } 
} 

Bootstrapper.cs

public class AppRootPathProvider : IRootPathProvider 
{ 
    private readonly IHostingEnvironment _environment; 

    public AppRootPathProvider(IHostingEnvironment environment) 
    { 
     _environment = environment; 
    } 
    public string GetRootPath() 
    { 
     return _environment.WebRootPath; 
    } 
} 

public class Bootstrapper : DefaultNancyBootstrapper 
{ 
    readonly IApplicationBuilder _app; 

    protected override IRootPathProvider RootPathProvider { get; } 

    public Bootstrapper(IApplicationBuilder app, IHostingEnvironment environment) 
    { 
     RootPathProvider = new AppRootPathProvider(environment); 
     _app = app; 
    } 

    protected override void ConfigureApplicationContainer(TinyIoCContainer container) 
    { 
     base.ConfigureApplicationContainer(container); 

     container.Register<IOptions<Endpoints>>(_app.ApplicationServices.GetService<IOptions<Endpoints>>()); 

     container.Register<IReservationService>(_app.ApplicationServices.GetService<IReservationService>()); 
    } 
} 
0

あなたが書いたミドルウェアは、アプリケーションの後にする必要があります。 UseDefaultFiles();およびapp.UseStaticFiles(); あなたはAPIリクエストのみを処理しています。それにも404エラーを処理すると、拡張子を持つ要求を処理しないように少しを書き換えるようにしてください:

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

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

いいえ、まだ成功404 – Reft

+0

悪いです。それは方法、それは私のために働く方法です:http://asp.net-hacker.rocks/2016/04/04/aspnetcore-and-angular2-part1.html おそらくそれはいくつかのNancyFX設定のため、私は一緒に働いたことはないそれ。 –

+0

私の答えを見て、あなたが異議を持っていれば教えてください:)ありがとう – Reft

0

あなたが最初await next();を削除する必要があります。それは/reservationsへのリクエストをの前に送り、の前にindex.htmlに書き換えます。

ナンシーに当たったら、/reservationsを処理できるかどうかを確認し、404ページを表示します。 Startup.csすべての要求のためのindex.htmlを返し

  • 追加モジュール修正

  • +0

    いいえ、まだ銀行のページは成功しません。また、コンソールエラーを出します:TypeError:error.jsonは関数ではありません – Reft

    +0

    私の答えを見て、あなたに異議があれば教えてください:)ありがとう – Reft

    関連する問題