wcf – 如何在(每次)Web API操作之前执行代码?

前端之家收集整理的这篇文章主要介绍了wcf – 如何在(每次)Web API操作之前执行代码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Web API接口,我正在尝试适应多租户架构.以前,我们有一个WCF模式,我们将一个参数client id传递给服务,然后将其存储在稍后的代码中.这意味着Client Id不必是传递给每个调用的第一个参数.

我想对Web API做同样的事情,即不要:

GetDocument(int clientId,int documentId)
GetDefault(int clientId)
GetImage(int clientId,int imageId)

才刚刚:

GetDocument(int documentId)
GetDefault()
GetImage(int imageId)

但我需要一些方法来做到以下几点:

>从路线获取clientId
>将此值放入我所拥有的状态对象中

所有在呼叫实际执行之前.我有点认为路线会被重写 – 我很好,路由必须有客户端ID,而不是我的API.因此,对GetDefault的调用可能如下所示:

/文档/ GetDefault / 1

虽然API是

GetDefault()

思考? TIA.

@R_404_323@

一种方法自定义ActionFilter.请参阅 here,虽然它与MVC有关,但概念与WebAPI相同:

ASP.NET MVC provides Action Filters for executing filtering logic
either before or after an action method is called. Action Filters are
custom attributes that provide declarative means to add pre-action and
post-action behavior to the controller’s action methods.

例如:

public class MyActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
           //....
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
           //....
        }
    }

并使用它来装饰你的API控制器/动作:

[MyActionFilter]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1","value2" };
    }
原文链接:https://www.f2er.com/html/225103.html

猜你在找的HTML相关文章