I have an ASP.net Core 2.1 Web API + Angular 6 web application created using Visual Studio 2017 Angular project template. Both the api and the Angular app are in the same web project. In development it runs inside IIS Express, while in production it uses IIS.
The app itself is working fine, but I'm having a hard time deploying it in a subfolder of an existing Web Site, configured as a new Web Application in IIS.
When I invoke the URLs as:
Angular is able to build the relative paths correctly and the application loads fine.
When I visit the URLs as (note the missing trailing slash):
IIS serves the index.html
web page (why? I would expect a 404!), but then Angular can't compute correctly the relative paths to the JS/CSS resources.
I tried using the Microsoft.AspNetCore.Rewrite
module to Redirect the ^$
path to /index.html
and it works, but it also rewrites the originally working requests, giving an ugly flash in the address bar:
http://localhost:52000/AngularWebApp/
=>
http://localhost:52000/AngularWebApp/index.html
=>
http://localhost:52000/AngularWebApp/#/dashboard
I tried to redirect to #/
but Chrome stops the requests with ERR_TOO_MANY_REDIRECTS.
Is there any solution to this problem? Please note that I would like a general solution which will allow me to publish the web app either under the subfolder or under its own domain without rebuilding the application.
Relevant snippets:
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
...
app.UseRewriter(new RewriteOptions()
.AddRedirect(@"^$", "/index.html", (int)HttpStatusCode.MovedPermanently));
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(...);
app.UseSpa(...);
...
}
index.html
<base href="./">
Thanks in advance for your help