在webapi中定义全局的过滤器要在app_start下WebApiConfig.cs中
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "push.api.v1",
routeTemplate: "v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Filters.Add(new PushFilter());
// Remove the JSON formatter //config.Formatters.Remove(config.Formatters.JsonFormatter); // Remove the XML formatter config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
在mvc中要在Global.asax中
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalFilters.Filters.Add(new DemoActionAttributeFilter() { Message = "Gloable" });
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
时间: 2025-01-22 17:16:23