2020-11-17 11:27:29 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-10-26 13:59:46 +01:00
|
|
|
|
2021-02-23 15:50:33 +01:00
|
|
|
namespace zero.Core.Routing
|
2020-11-17 11:27:29 +01:00
|
|
|
{
|
|
|
|
|
public class ZeroRoutesTransformer : DynamicRouteValueTransformer
|
|
|
|
|
{
|
2021-10-06 16:12:40 +02:00
|
|
|
IRouteResolver RouteResolver;
|
2020-10-26 13:59:46 +01:00
|
|
|
|
2021-10-06 16:12:40 +02:00
|
|
|
public ZeroRoutesTransformer(IRouteResolver routeResolver)
|
2020-11-17 11:27:29 +01:00
|
|
|
{
|
2021-10-06 16:12:40 +02:00
|
|
|
RouteResolver = routeResolver;
|
2020-11-17 11:27:29 +01:00
|
|
|
}
|
2020-10-26 13:59:46 +01:00
|
|
|
|
|
|
|
|
|
2020-11-17 11:27:29 +01:00
|
|
|
public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
|
|
|
|
|
{
|
2021-11-06 16:27:04 +01:00
|
|
|
IRouteModel route = httpContext.Features.Get<IRouteModel>();
|
2021-09-30 10:53:01 +02:00
|
|
|
|
|
|
|
|
if (route != null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-10-26 13:59:46 +01:00
|
|
|
|
2021-10-06 16:12:40 +02:00
|
|
|
route = await RouteResolver.ResolveUrl(httpContext);
|
2021-09-29 15:46:23 +02:00
|
|
|
|
2020-11-17 11:27:29 +01:00
|
|
|
if (route == null)
|
|
|
|
|
{
|
2021-09-29 13:30:07 +02:00
|
|
|
return null;
|
2020-11-17 11:27:29 +01:00
|
|
|
}
|
2020-10-26 13:59:46 +01:00
|
|
|
|
2021-09-30 10:53:01 +02:00
|
|
|
httpContext.Features.Set(route);
|
|
|
|
|
|
2021-11-06 16:27:04 +01:00
|
|
|
RouteEndpoint endpoint = RouteResolver.MapEndpoint(route);
|
2020-10-26 13:59:46 +01:00
|
|
|
|
2021-07-20 12:56:45 +02:00
|
|
|
if (endpoint == null)
|
|
|
|
|
{
|
2021-09-29 13:30:07 +02:00
|
|
|
return null;
|
2021-07-20 12:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-17 11:27:29 +01:00
|
|
|
values["controller"] = endpoint.Controller;
|
|
|
|
|
values["action"] = endpoint.Action;
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|