在asp.net core 2项目中使用路径提供角度

问题描述 投票:0回答:1

我有一个角色项目,我使用cli“ng build --prod”构建并放入我的asp.net核心项目的wwwroot目录中,我希望为该站点提供服务。 asp项目没有我正在使用的MVC控制器,所以我不必考虑这一点。

我将应用程序设置为使用默认文件和静态文件。当我导航到服务器根目录时,这给了我角度应用程序,在本例中,localhost:5000 =>(deliver)index.html。从那里我可以使用应用程序和路由工作。

但是当我尝试手动链接到任何路线时(例如,键入http://localhost:5000/quality-test-list)我只得到一个没有任何内容的白页。

我的猜测是服务器端路由禁止这个。我看过标准的“dotnet new angular”项目文件,但我无法弄清楚。

我的启动文件:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseDefaultFiles();
        app.UseStaticFiles();

    }
}
c# angular asp.net-core
1个回答
0
投票

在您的配置部分中,使用app.use方法将所有404配置为您的index.html

    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();
              }
    })
    .UseDefaultFiles(new DefaultFilesOptions { DefaultFilesName = new List<string>{ "index.html" } })
    .UseStaticFiles(new StaticFilesOptions 
                    {
                       FileProvider = new PhysicalFileProvider(
                           Path.Combine(Directory.GetCurrentDiretory(), "@wwwroot")),
                           RequestPath = new PathString("")
    })
    .UseMvc();
© www.soinside.com 2019 - 2024. All rights reserved.