ActionFiler

ActionFilter只应用于MVC模式,使用UseMvc开启Mvc中间件;在ControllerBuilder中创建Filter. 支持正则表达式与统配符*.

  1. func CreateCustomBuilder() *abstractions.HostBuilder {
  2. configuration := abstractions.NewConfigurationBuilder().
  3. AddEnvironment().
  4. AddYamlFile("config").Build()
  5. return yoyogo.NewWebHostBuilder().
  6. UseConfiguration(configuration)
  7. Configure(func(app *yoyogo.WebApplicationBuilder) {
  8. app.UseMvc(func(builder *mvc.ControllerBuilder) {
  9. builder.AddController(contollers.NewUserController)
  10. builder.AddFilter("/v1/user/info", &contollers.TestActionFilter{})
  11. })
  12. })
  13. }

ActionFilter定义

它实现了IActionFilter接口,如下是它的实例定义.

  1. type TestActionFilter struct {
  2. }
  3. func (f *TestActionFilter) OnActionExecuting(context mvc.ActionFilterContext) bool {
  4. fmt.Println("TestActionFilter OnActionExecuting")
  5. return false
  6. }
  7. func (f *TestActionFilter) OnActionExecuted(context mvc.ActionFilterContext) {
  8. fmt.Println("TestActionFilter OnActionExecuted")
  9. }