Files
mixtape/zero.Web/ZeroApplicationBuilderExtensions.cs
T

94 lines
2.7 KiB
C#
Raw Normal View History

2020-04-15 14:09:52 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
2021-07-22 15:47:40 +02:00
using System;
2020-04-15 14:09:52 +02:00
using zero.Core.Extensions;
using zero.Core.Options;
2021-02-23 15:50:33 +01:00
using zero.Core.Routing;
2020-11-05 00:46:18 +01:00
using zero.Web.Middlewares;
2020-04-15 14:09:52 +02:00
namespace zero.Web
{
public static class ZeroApplicationBuilderExtensions
{
2020-11-01 23:23:36 +01:00
public static IApplicationBuilder UseZero(this IApplicationBuilder app)
2020-04-15 14:09:52 +02:00
{
IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
2020-04-15 14:09:52 +02:00
string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
2021-04-02 15:14:30 +02:00
if (!options.Routing.ErrorReexecutionPath.IsNullOrEmpty())
{
app.UseStatusCodePagesWithReExecute(options.Routing.ErrorReexecutionPath.EnsureStartsWith('/'), "?statusCode={0}");
}
2020-11-17 11:27:31 +01:00
app.UseMiddleware<ZeroContextMiddleware>();
app.UseStaticFiles();
2020-11-06 19:18:56 +01:00
2020-04-15 14:09:52 +02:00
// map backoffice
2020-11-17 11:27:29 +01:00
app.UseWhen(ctx => ctx.Request.Path.ToString().StartsWith(path), builder =>
{
builder.UseRouting();
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:29 +01:00
builder.UseAuthentication();
builder.UseAuthorization();
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:31 +01:00
//builder.UseMiddleware<ZeroMiddleware>(options);
2020-11-17 11:27:29 +01:00
builder.UseEndpoints(endpoints =>
{
2020-11-17 11:27:31 +01:00
// setup route
//endpoints.MapControllerRoute(
// name: "setup",
// pattern: path + "/setup",
// defaults: new
// {
// controller = "ZeroSetup",
// action = "Index"
// }
//);
2020-11-17 11:27:29 +01:00
//// routes for API
//endpoints.MapControllerRoute(
// name: "api",
// pattern: path + "/api/{controller}/{action}/{id?}"
//);
2020-04-15 14:09:52 +02:00
2020-11-17 11:27:29 +01:00
// fallbacks for SPA
endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroBackoffice");
});
});
2020-04-15 14:09:52 +02:00
2020-11-06 19:18:56 +01:00
return app;
}
2020-10-27 01:04:23 +01:00
2020-11-06 19:18:56 +01:00
2020-11-17 11:27:31 +01:00
public static IApplicationBuilder UseZeroRoutes(this IApplicationBuilder app)
2020-11-06 19:18:56 +01:00
{
2021-07-22 13:05:05 +02:00
IZeroOptions options = app.ApplicationServices.GetService<IZeroOptions>();
return app.UseEndpoints(endpoints =>
2020-10-26 13:59:46 +01:00
{
endpoints.MapDynamicControllerRoute<ZeroRoutesTransformer>("{**url}", state: null, order: 10);
2021-07-22 13:05:05 +02:00
//if (options.Routing.NotFoundEndpoint != null)
//{
// endpoints.MapFallbackToController(options.Routing.NotFoundEndpoint.Action, options.Routing.NotFoundEndpoint.Controller);
//}
2020-11-17 11:27:31 +01:00
});
2021-07-22 15:47:40 +02:00
//return app.Use(async (ctx, next) =>
//{
// await next();
// if (ctx.Response.StatusCode == 404 && !ctx.Response.HasStarted)
// {
// Console.WriteLine("NotFound0: " + ctx.Request.Path);
// //Re-execute the request so the user gets the error page
// //ctx.Request.Path = "/Pages404";
// await next();
// }
//});
2020-04-15 14:09:52 +02:00
}
}
}