C
C#3y ago
NonUnique

Setting response code for SPA static file to 404?

Hi, I have a problem - I need ngsw.config missing file to return 404 instead of SPA 200 with index.html. I tried it setting with OnPrepareResponse but apparently you cannot change status code there? SPA site is hosted on IIS. How can I make non existing file ngsw.config return 404 status code? Startup.cs part (the one if(context.File.Name.StartsWith("ngsw.json"))):
// Client
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}

app.UseSpa(spa =>
{
spa.Options.SourcePath = "../ClientApp";

spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
// force rechecking Angular's index.html to prevent using index.html that references old scripts
if (context.File.Name == "index.html")
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
context.Context.Response.Headers.Add("Pragma", "no-cache");
context.Context.Response.Headers.Add("Expires", "0");
}

// to get rid of running service workers return 404 on cache bust as fail-safe deactivation
if (context.File.Name.StartsWith("ngsw.json"))
{
context.Context.Response.StatusCode = 404;
}
},
};

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
// Client
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}

app.UseSpa(spa =>
{
spa.Options.SourcePath = "../ClientApp";

spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
// force rechecking Angular's index.html to prevent using index.html that references old scripts
if (context.File.Name == "index.html")
{
context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate");
context.Context.Response.Headers.Add("Pragma", "no-cache");
context.Context.Response.Headers.Add("Expires", "0");
}

// to get rid of running service workers return 404 on cache bust as fail-safe deactivation
if (context.File.Name.StartsWith("ngsw.json"))
{
context.Context.Response.StatusCode = 404;
}
},
};

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server