使用ASP .Net Core(非mvc)进行Angular 2路由

Met*_*ian 2 asp.net asp.net-mvc asp.net-core angular

我正在尝试使用.net核心和Angular 2设置路由,但路由无法解析,因为服务器已解析它们.

我见过的一个解决方案是注册一个到你的家庭控制器的默认路由......但我没有任何MVC控制器.

我已将此添加到我的主要组件(并完成所有其他路由器先决条件)

@RouteConfig([
    { path: '/', name: 'Table', component: TableComp, useAsDefault: true },
    { path: '/login', name: 'Login', component: LoginComp }
])
Run Code Online (Sandbox Code Playgroud)

我在startup.cs中有这些:

在ConfigureServices()中

services.AddMvc();
Run Code Online (Sandbox Code Playgroud)

在Configure()中

app.UseMvc();
Run Code Online (Sandbox Code Playgroud)

但由于我实际上并没有使用任何MVC控制器或注册任何MVC路由,我不知道如何在浏览器而不是服务器中解析我的角度路由,以及为什么他们不只是在做事情...

Fáb*_*ira 13

以下配置应适合在.NET Core中使用客户端路由的大多数项目:

DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("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();
    }
})
.UseCors("AllowAll")
.UseMvc()
.UseDefaultFiles(options)
.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)