Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active June 30, 2021 07:13
Show Gist options
  • Save gistlyn/c8ad3948cabbc57795d084cce0142077 to your computer and use it in GitHub Desktop.
Save gistlyn/c8ad3948cabbc57795d084cce0142077 to your computer and use it in GitHub Desktop.
filters
public class AppHost : AppHostBase
{
public AppHost() : base("Web",typeof(MyServices).Assembly){}
public override void Configure(Container container)
{
GlobalRequestFilters.Add((req, res, requestDto) => {
var sessionId = req.GetCookieValue("user-session");
if (sessionId == null)
res.ReturnAuthRequired();
});
RegisterTypedRequestFilter<Resource>((req, res, dto) =>
{
var route = req.GetRoute();
if (route?.Path == "/tenant/{Name}/resource")
dto.SubResourceName = "CustomResource";
});
}
}
public class MyServices : Service
{
[NoNumbersInPath]
public object Any(Hello request)
{
return new HelloResponse {
Result = $"Hello, {request.Name}!"
};
}
}
public class NoNumbersInPathAttribute : RequestFilterAttribute
{
public override void Execute(IRequest req, IResponse res,
object requestDto)
{
if (Regex.IsMatch(req.PathInfo, "[0-9]"))
throw HttpError.BadRequest("No numbers");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment