问题描述
我要寻找一种方式来拦截/抢匹配的路由前提出该要求。例如,我有多个控制器和设置的路线,但我想一些机制,其中被击中的路径方法之前将受到打击。这将是非常preferable如果这个机制能够得到被送到该路由PARAMS。
I am looking for a way to intercept/grab the request being made before matching to a route. For example, I have multiple controllers and routes set up, but I want some mechanism in which will be hit before the route method is hit. It would be highly preferable if this mechanism were able to get the route params that were sent.
我所期待的(但也许不是正在网页API深谙我用错误的关键词搜索)我已经无法找到类似的东西。
I have been unable to find something similar to what I am looking for (but perhaps not being well versed in Web API I am searching with the wrong keywords).
推荐答案
您需要的是采取行动的过滤器。您可以直接适用于任何过滤器,控制器,属性,用行动过滤器需要说明的是,在这一点上,控制器路由已经知道,但你仍然可以控制(很像AOP),如果操作方法可以执行与否:
What you need is action filters. You can apply action filters directly to controllers as attributes, the caveat with Action filters is that at this point the controller route is already known but you can still control (very much like AOP) if the action method can be executed or not:
看看如何使用一个动作过滤器,在这种情况下进行记录:
Look at how you can use an action filter, in this case for logging:
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
Log(actionExecutedContext.ActionContext.RequestContext.RouteData);
base.OnActionExecuted(actionExecutedContext);
}
private void Log(System.Web.Http.Routing.IHttpRouteData httpRouteData)
{
var controllerName = "controller name";
var actionName = "action name";
var message = String.Format("controller:{0}, action:{1}", controllerName, actionName);
Debug.WriteLine(message, "Action Filter Log");
}
}
How要记录的操作方法是的WebAPI
您也可以使用消息处理程序,控制器解决之前所执行的:
You can also use message handlers, which are executed before the controller is resolved:
这篇关于拦截路线匹配发生之前所有的WebAPI调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!