如何添加全局 ASP.Net Web Api 过滤器? [英] How to add global ASP.Net Web Api Filters?

查看:28
本文介绍了如何添加全局 ASP.Net Web Api 过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 Web Api 过滤器(使用 System.Web.Http.Filters.ActionFilterAttribute),但我无法让它在 ASP.Net MVC 4 中工作.我尝试添加RegisterGlobalFilters() 方法,但没有用.

I've created a Web Api filter (using System.Web.Http.Filters.ActionFilterAttribute) but I am unable to get it to work inside of ASP.Net MVC 4. I tried adding it to the RegisterGlobalFilters() method but that didn't work.

那么,如果使用托管在 ASP.Net MVC 中的 Web Api,如何注册过滤器?

So if one is using Web Api hosted in ASP.Net MVC how does one register filters?

推荐答案

以下代码,在我的 Global.asax 中,适用于我:

The following code, in my Global.asax, works for me:

public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
  filters.Add(new MyWebApiFilter());
}

protected void Application_Start()
{
  RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
}

这篇关于如何添加全局 ASP.Net Web Api 过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆