Skip to content

Instantly share code, notes, and snippets.

@timdows
Created March 7, 2019 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdows/33deb8a2d2c4ad0e64d2dfa288c69869 to your computer and use it in GitHub Desktop.
Save timdows/33deb8a2d2c4ad0e64d2dfa288c69869 to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace DAB.Api
{
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}
public IConfiguration Configuration { get; }
public IHostingEnvironment Environment { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
...
bool isWwwRequest(HttpContext context) => context.Request.Host.Host.ToLower().Contains("www");
var rootPath = Environment.IsDevelopment()
? "../AngularApps"
: "AngularApps";
var aboutPath = Path.Combine(Environment.ContentRootPath, $"{rootPath}/about");
var webPath = Path.Combine(Environment.ContentRootPath, $"{rootPath}/web");
app.UseWhen(context => isWwwRequest(context), appBuilder =>
{
appBuilder.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(aboutPath),
});
appBuilder.Run(async context =>
{
await context.Response.SendFileAsync(Path.Combine(aboutPath, "index.html"));
return;
});
});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(webPath),
});
app.Run(async context =>
{
await context.Response.SendFileAsync(Path.Combine(webPath, "index.html"));
return;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment