如何在 OwinSelfHost 中使用 ActionFilters
How To Use ActionFilters in OwinSelfHost
我使用 microsoft.owin 在 ConsoleApplication 中自行托管 WebApi
但 ActionFilters 不会在应用程序中触发。这是我的动作过滤器:
public class FilterA : Attribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (!filterContext.RouteData.GetRequiredString("controller").Equals("account", StringComparison.CurrentCultureIgnoreCase) //long condition evaluating to true
//logic controlling these filters dont apply to Foo/Bar and Foo/Baz
&& (!filterContext.RouteData.GetRequiredString("controller").Equals("Foo", StringComparison.CurrentCultureIgnoreCase)
|| (!filterContext.RouteData.GetRequiredString("action").Equals("Bar", StringComparison.CurrentCultureIgnoreCase)
&& !filterContext.RouteData.GetRequiredString("action").Equals("Baz", StringComparison.CurrentCultureIgnoreCase))
)
)
{
filterContext.Result = new RedirectResult("~/Foo/Bar");
}
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.RouteData.GetRequiredString("controller").Equals("account", StringComparison.CurrentCultureIgnoreCase) //long condition evaluating to true
//logic controlling these filters dont apply to Foo/Bar and Foo/Baz
&& (!filterContext.RouteData.GetRequiredString("controller").Equals("Foo", StringComparison.CurrentCultureIgnoreCase)
|| (!filterContext.RouteData.GetRequiredString("action").Equals("Bar", StringComparison.CurrentCultureIgnoreCase)
&& !filterContext.RouteData.GetRequiredString("action").Equals("Baz", StringComparison.CurrentCultureIgnoreCase))
)
)
{
filterContext.Result = new RedirectResult("~/Foo/Bar");
}
}
}
并像这样使用它,但 FilterA 不触发。有什么问题
[FilterA]
[Route("RpTDAccounting")]
[HttpGet]
public IHttpActionResult RpTDAccounting()
{
return Ok();
}
应该使用 System.Web.Http.Filters 命名空间而不是 System.web.mvc
和 impelment IActionFilter 像这样:
public class FilterA : Attribute, IActionFilter
{
public bool AllowMultiple => throw new NotImplementedException();
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
throw new NotImplementedException();
}
}
我使用 microsoft.owin 在 ConsoleApplication 中自行托管 WebApi
但 ActionFilters 不会在应用程序中触发。这是我的动作过滤器:
public class FilterA : Attribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (!filterContext.RouteData.GetRequiredString("controller").Equals("account", StringComparison.CurrentCultureIgnoreCase) //long condition evaluating to true
//logic controlling these filters dont apply to Foo/Bar and Foo/Baz
&& (!filterContext.RouteData.GetRequiredString("controller").Equals("Foo", StringComparison.CurrentCultureIgnoreCase)
|| (!filterContext.RouteData.GetRequiredString("action").Equals("Bar", StringComparison.CurrentCultureIgnoreCase)
&& !filterContext.RouteData.GetRequiredString("action").Equals("Baz", StringComparison.CurrentCultureIgnoreCase))
)
)
{
filterContext.Result = new RedirectResult("~/Foo/Bar");
}
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.RouteData.GetRequiredString("controller").Equals("account", StringComparison.CurrentCultureIgnoreCase) //long condition evaluating to true
//logic controlling these filters dont apply to Foo/Bar and Foo/Baz
&& (!filterContext.RouteData.GetRequiredString("controller").Equals("Foo", StringComparison.CurrentCultureIgnoreCase)
|| (!filterContext.RouteData.GetRequiredString("action").Equals("Bar", StringComparison.CurrentCultureIgnoreCase)
&& !filterContext.RouteData.GetRequiredString("action").Equals("Baz", StringComparison.CurrentCultureIgnoreCase))
)
)
{
filterContext.Result = new RedirectResult("~/Foo/Bar");
}
}
}
并像这样使用它,但 FilterA 不触发。有什么问题
[FilterA]
[Route("RpTDAccounting")]
[HttpGet]
public IHttpActionResult RpTDAccounting()
{
return Ok();
}
应该使用 System.Web.Http.Filters 命名空间而不是 System.web.mvc
和 impelment IActionFilter 像这样:
public class FilterA : Attribute, IActionFilter
{
public bool AllowMultiple => throw new NotImplementedException();
public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
throw new NotImplementedException();
}
}