ZeroController as base for frontend

This commit is contained in:
2020-10-27 00:24:49 +01:00
parent 67675ee46d
commit 73210948aa
3 changed files with 55 additions and 5 deletions
+44
View File
@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zero.Core.Entities;
using zero.Core.Routing;
namespace zero.Web.Controllers
{
public abstract class ZeroController : ZeroController<IResolvedRoute>
{
}
public abstract class ZeroController<T> : Controller where T : IResolvedRoute
{
protected IApplication Application { get; set; }
protected virtual T Route { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.RouteData.Values.TryGetValue("zero.app", out object app))
{
Application = (IApplication)app;
}
if (filterContext.RouteData.Values.TryGetValue("zero.route", out object route))
{
if (!(route is T))
{
throw new InvalidCastException($"Could not cast IResolvedRoute to {typeof(T)}");
}
Route = (T)route;
}
base.OnActionExecuting(filterContext);
}
}
}