VEU.WathBody/3-app/1-lib/Kit.Helpers/Routes/RouteFluent.cs

72 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
namespace Kit.Helpers.Routes
{
public class RouteFluent : IEndPointFluent<RouteFluent>
{
public Route Route { get; set; }
public RouteFluent NeedHttpGet(bool required = true)
{
this.Route.Constraints.Add("httpMethod", new HttpMethodRouteConstraint("GET"));
return this;
}
public RouteFluent NeedHttpPost(bool required = true)
{
this.Route.Constraints.Add("httpMethod", new HttpMethodRouteConstraint("POST"));
return this;
}
public RouteFluent NeedSecurityKey(string securityKeyName)
{
this.Route.DataTokens["APP_SecurityKeyName"] = securityKeyName;
return this;
}
public RouteFluent WithDefaults(object defaults)
{
if (defaults == null) return this;
foreach (var pair in new RouteValueDictionary(defaults))
{
this.Route.Defaults.Add(pair.Key, pair.Value);
}
return this;
}
public RouteFluent WithConstraints(object constraints)
{
if (constraints == null) return this;
foreach (var pair in new RouteValueDictionary(constraints))
{
this.Route.Constraints.Add(pair.Key, (IRouteConstraint)pair.Value);
}
return this;
}
public RouteFluent WithNamespaces(string[] namespaces)
{
if (namespaces == null) return this;
this.Route.DataTokens["Namespaces"] = namespaces;
return this;
}
public RouteFluent WithDataTokens(object dataTokens)
{
if (dataTokens == null) return this;
foreach (var pair in new RouteValueDictionary(dataTokens))
{
this.Route.DataTokens[pair.Key] = pair.Value;
}
return this;
}
public RouteFluent JsonResultModel()
{
this.Route.DataTokens["jsonResultModel"] = true;
return this;
}
}
}