Last active
June 30, 2021 07:13
-
-
Save gistlyn/c8ad3948cabbc57795d084cce0142077 to your computer and use it in GitHub Desktop.
filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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