如何增加全球ASP.Net网页API过滤器? [英] How to add global ASP.Net Web Api Filters?

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

问题描述

我创建了一个网页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?

推荐答案

以下code,在我的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网页API过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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