C# 为什么我的 ASP.NET Web API ActionFilterAttribute OnActionExecuting 没有触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12992722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing?
提问by slashp
I'm trying to implement what's seen here: http://www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/but I'm having an issue with my NhSessionManagementAttribute.
我正在尝试实现这里看到的内容:http: //www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/但我的NhSessionManagementAttribute.
I've set breakpoints on my OnActionExecuting(HttpActionContext actionContext)to see whether the function was ever being called -- it wasn't.
我已经在 myOnActionExecuting(HttpActionContext actionContext)上设置了断点,以查看该函数是否被调用过——它没有被调用。
I double-checked my global.asax.csfile & found I am in fact registering the ActionFilterwith:
我仔细检查了我的global.asax.cs文件,发现我实际上正在注册ActionFilter:
GlobalConfiguration.Configuration.Filters.Add(new NhSessionManagementAttribute());
I have also decorated both my controller class itself, as well as its actions with the attribute to no avail:
我还装饰了我的控制器类本身,以及它的属性,但无济于事:
public class ClientsController : ApiController {
static readonly ClientRepository repository = new ClientRepository();
[NhSessionManagement]
public IEnumerable<Client> GetAllClients() {
return repository.GetAll();
}
[NhSessionManagement]
public Client GetClient(int id) {
Client client = repository.Get(id);
if (client == null) {
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.NotFound)
);
}
return client;
}
}
Why would this action filter not be firing any of the events within?
为什么此操作过滤器不会触发其中的任何事件?
采纳答案by Troy Dai
If you're working in a project contains both MVC and WebAPI assembilies, could you check what's the namespace your ActionFilterAttribute's namespace. It's fairly confusing cause there are two ActionFilterAttributes under both:
如果您在一个包含 MVC 和 WebAPI 程序集的项目中工作,您能否检查一下 ActionFilterAttribute 的命名空间是什么命名空间。这相当令人困惑,因为两者下都有两个 ActionFilterAttributes:
- WebAPI: System.Web.Http.Filters
- MVC: System.Web.Http.Mvc
- WebAPI:System.Web.Http.Filters
- MVC:System.Web.Http.Mvc
回答by ProVega
The answer above definitely helped me - to save others some time... here is explicitly the difference.
上面的答案肯定对我有帮助 - 为其他人节省一些时间......这显然是不同之处。
Standard MVC Controllers use:
标准 MVC 控制器使用:
// System.Web.Mvc
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
OData HTTP Controllers use:
OData HTTP 控制器使用:
// System.Web.Http.Filters;
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
}
回答by Johnie Karr
For anyone else who comes across this, ActionFilterAttribute will not fire when calling YourController.YourAction from your UnitTest.
对于遇到此问题的任何其他人,从 UnitTest 调用 YourController.YourAction 时,ActionFilterAttribute 不会触发。
[TestMethod]
public void RevokeSiteAdmin_SessionOver()
{
FakeDbContext db = new FakeDbContext();
YourController controller = new YourController(db);
var result = controller.YourAction();
//Some Assertions
}
In the TestMethod above, any ActionFilterAttributes on YourController.YourAction will not be called. However; if you call YourController.YourAction from a browser, your ActionFilterAttribute will be called.
在上面的 TestMethod 中,不会调用 YourController.YourAction 上的任何 ActionFilterAttributes。然而; 如果您从浏览器调用 YourController.YourAction,您的 ActionFilterAttribute 将被调用。
This is true for at least WebApi, but I don't know if it applies to MVC.
至少对于 WebApi 是这样,但我不知道它是否适用于 MVC。
回答by AjitChahal
Here is the complete Implementation:
这是完整的实现:
public class AllowCrossSiteJsonAttribute : System.Web.Mvc.ActionFilterAttribute
{
public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
{
if (filterContext.HttpContext != null && filterContext.HttpContext.Response != null && filterContext.HttpContext.Request != null && filterContext.HttpContext.Request.UrlReferrer != null)
{
var allowedCrossDomains = TypeSafeConfigurationManager.GetValueString("allowedCrossDomains", "none");
var allowedHosts = allowedCrossDomains.Split(',');
var requestHost = filterContext.HttpContext.Request.UrlReferrer.GetLeftPart(UriPartial.Authority);
if (allowedHosts.Contains(requestHost.ToLower()))
{
filterContext.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", requestHost);
}
}
base.OnActionExecuted(filterContext);
}
}
public class AllowCrossSiteJsonForWebApiAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null && actionExecutedContext.Request != null &&
actionExecutedContext.Request.Headers.Referrer != null)
{
var allowedCrossDomains = TypeSafeConfigurationManager.GetValueString("allowedCrossDomains", "none");
var allowedHosts = allowedCrossDomains.Split(',').ToList();
var requestHost = actionExecutedContext.Request.Headers.Referrer.GetLeftPart(UriPartial.Authority);
if (allowedHosts.Contains(requestHost.ToLower()))
{
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", requestHost);
}
base.OnActionExecuted(actionExecutedContext);
}
}
}
回答by Baran
For WebApi, you should install Microsoft.AspNet.WebApi.Core from nuget. For MVC you can use System.Web.MVC.
对于 WebApi,您应该从 nuget 安装 Microsoft.AspNet.WebApi.Core。对于 MVC,您可以使用 System.Web.MVC。
回答by bendecko
My problem was much more simple:
我的问题要简单得多:
Check your Controller is decorated with <actionPreProcessActivitiesAttribute()> _
检查您的控制器是否装饰有 <actionPreProcessActivitiesAttribute()> _

