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

扎卡里亚斯·蒙特尔

我有一个 angular 项目,我使用 cli "ng build --prod" 构建它,并将其放入我想要为该站点提供服务的 asp.net 核心项目的 wwwroot 目录中。asp 项目没有我正在使用的 MVC 控制器,所以我不必考虑这一点。

我将应用程序设置为使用默认文件和静态文件。当我导航到服务器根目录时,这为我提供了 angular 应用程序,在本例中为 localhost:5000 => (delivers) 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();

    }
}
埃尔默·丹塔斯

在您的配置部分,使用该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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Asp.net core 2前缀路由

在测试项目中使用ASP.NET Core的ConfigurationBuilder

如何在 Asp.net Core 项目中使用 SoapCore 在项目路由文件夹中公开 wsdl

在ASP.NET Core 3.0 Web API项目中使用WebApiContrib.Core.Formatter.Csv

在ASP.NET Core 2中的layout.cshtml中使用和路由较少的文件

在ASP .Net Core项目中使用.Net用户控件(.ascx)的可能性

asp.net core2路由问题

ASP.Net MVC Core 2-区域路由

带角度6模板的ASP.NET Core 2

asp-net core2新的Web项目问题

ASP.Net Core 2无法解析类型服务

Asp.net Core 2-如何在Asp.net Core 2.0中使用ServiceLocator

如何在Visual Studio ASP.NET Core项目中使用语义UI反应?

在没有 EF Core 参考的 ASP.NET 项目中使用 EF 存储?

在ASP.NET Core项目中使用没有实体框架的SQLLite数据库

如何在ASP.NET Core项目中使用MStest测试Ok()结果

在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式)

Asp.Net MVC Core 2 - 在 _Layout 上使用 ViewBag

无法使用ASP.net Core 2添加迁移

是否可以从针对.NET Standard 2.x的库中使用ASP .Net Core 3.1?

如何在Asp.Net Core 2.1项目中使用EF Core 2.2.4数据库

ASP.NET Core 2:缺少ApplicationInsights

ASP.NET Core 2:UserManager与DbContext

C#ASP.NET Core 2 HashAlgorithm

ASP.NET Core RC2项目-什么是“服务”文件夹?

在 ASP.net Core MVC 2 中使用 ASP.net MVC 5 脚手架

可以在Windows服务定位中托管ASP.NET Core 2 Web API NET Core 2应用程序

在Angular 6和.net core 2项目中使用ngx-toastr

在ASP.NET Core中使用NVarChar