所有ASPNET核心路由都转到DefaultFiles

时间:2017-04-19 19:25:06

标签: c# asp.net-core

我希望/api/*以外的所有请求都能点击wwwroot文件夹中的默认文件,因为我正在运行spa!

我配置了这个:

app.UseIdentityServer();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = identityServerAppOptions.Value.Authority,
    RequireHttpsMetadata = false,

    ApiName = "all",

});
app.UseDefaultFiles(new DefaultFilesOptions
{
    DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        ctx.Context.Response.Headers.Append("Cache-Control", "no-cache");
    }
});
app.UseMvc(routes => routes.MapRoute(
        name: "default",
        template: "api/{controller=Details}/{id?}"));

但它并没有将/Account/Login路由到静态文件位置的默认文件。

我尝试在静态文件选项上使用RequestPath,但这听起来不是正确的事情!

1 个答案:

答案 0 :(得分:3)

可以使用ts-loader和正则表达式有选择地将请求网址重写为/index.html

要跳过以/api/*开头的所有请求,请使用regexp:@"^(?!api/).*$"

要跳过以/api/*开头的所有请求以及包含点.的所有请求:@"^(?!api/|.*\..*).*$"

var options = new RewriteOptions().AddRewrite(@"^(?!api/|.*\..*).*$", "index.html", true);
app.UseRewriter(options);

//should be after a rewriter in middleware pipeline
app.UseDefaultFiles();
app.UseStaticFiles();

P.S。我想,正则表达式可以通过优化。但这个概念是一样的。