简体   繁体   English

如何在Asp.net Web API 2中在运行时的每个请求中添加配置设置?

[英]How to add configuration setting with every request on runtime in Asp.net Web api 2?

I have a web api 2 application where I am trying to configure somevalue on runtime for every request. 我有一个Web API 2应用程序,我在其中尝试为每个请求在运行时配置一些值。 So basically my application needs to look up the value from other service api which changes about 15 times during the day based on that it needs to log the paralytics (the requirement sounds bit wired but that's what it is!). 因此,基本上我的应用程序需要从其他服务api查找值,该值在一天之内会变化约15倍,这是因为它需要记录瘫痪事件(这个要求听起来有些奇怪,但这就是事实!)。

1st Approach: I thought I can retrieve the value from Owin startup class but not sure if I can make it configurable on every request. 第一种方法:我以为可以从Owin启动类中检索值,但是不确定是否可以在每个请求中将其配置为可配置的。

2nd Approach: The another way I am thinking to do is to have the attribute for every action but that means I will have to remember to apply that attribute to every new action that I create. 第二种方法:我想做的另一种方法是为每个动作都具有属性,但这意味着我将必须记住将该属性应用于我创建的每个新动作。

So I am looking for best approach to handle this as I am new to web api! 所以我正在寻找最好的方法来解决这个问题,因为我是Web api的新手!

You can add global filter on owin startup which will be applied to each request: 您可以在启动文件时添加全局过滤器,该过滤器将应用于每个请求:

    public class OwinConfiguration
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            config.Filters.Add(new CustomActionFilterAttribute());

        }
    }

Where CustomActionFilterAttribute inherited from ActionFilterAttribute; 其中CustomActionFilterAttribute继承自ActionFilterAttribute;

Links: 链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM