Web API 2 访问服务控制器:来自类的 ApiController 公共变量

本文关键字:ApiController 变量 API 访问 服务 控制器 Web | 更新日期: 2023-09-27 18:34:19

嘿,我试图弄清楚如何从服务控制器访问变量:ApiController是这样的:

namespace WebApi.App.Controllers
{
    public class ServiceController : ApiController
    {
        string outputFile = "F:''debugData''debug.txt";
        public bool isDebuging = false;
        ...etc etc

我试图得到的是isDebuging值,但在我的类文件中

namespace WebApi.App.Models
{
    public class checkEnviroment
    {
        public string checkEnviroment()
        {
            WebApi.App.Controllers.ServiceController["isDebuging"] = true;
            etc etc...

这可能做到吗?我似乎找不到正确的语法来从服务控制器获取或设置值:ApiController

任何帮助都会很棒!

Web API 2 访问服务控制器:来自类的 ApiController 公共变量

该检查环境应该是一个ActionFilterAttribute

public class CheckEnvironmentFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(HttpActionContext actionContext)
     {
          // Use the "as" cast to don't throw an invalid cast exception
          // if this attribute is applied to the wrong controller...
          ServiceController serviceController =
                   actionContext.ControllerContext.Controller as ServiceController;
          if(serviceController != null)
          {
                serviceController.IsDebugging = true;
          }
     }
}

现在将整个过滤器属性作为常规 C# 属性添加到您的ServiceController

[CheckEnvironmentFilter]
public class ServiceController : ApiController
...

。而所谓的 filter 方法将在整个 API 控制器执行任何操作之前被命中。

顺便说一句,我将设计一个界面IDebuggable如下:

public interface IDebuggable
{
     bool IsDebugging { get; set; }
}

。我会在任何可能需要整个动作过滤器才能工作的控制器上实现它:

[CheckEnvironmentFilter]
public class ServiceController : ApiController, IDebuggable
{
     public bool IsDebugging { get; set; }
}

。最后,我将重构所谓的过滤器以将控制器转换为IDebuggable

public class CheckEnvironmentFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(HttpActionContext actionContext)
     {
          // Use the "as" cast to don't throw an invalid cast exception
          // if this attribute is applied to the wrong controller...
          IDebuggable debuggableController =
                   actionContext.ControllerContext.Controller as IDebuggable;
          if(debuggableController != null)
          {
                debuggableController.IsDebugging = true;
          }
     }
}

这比#1方法更好,因为现在CheckEnvironmentFilterAttribute将支持任何实现IDebuggable的控制器。

使属性isDebugging静态可能会有所帮助ServiceController.isDebugging = true;但简单的问题是你为什么需要它。如果需要全局属性,可以使用会话。

你可能做错了。这几种选择应该可以帮助您入门。最后两个选项非常适合单元测试。

#ifdef 调试

如果你想要一些只在调试版本中可见的调试代码,你可以使用 DEBUG 符号。仅当您在Visual Studio项目中选中了"复选框以定义DEBUG符号时,这才有效,默认情况下会选中该复选框。示例代码

#ifdef DEBUG
    // your code
#endif

将值注入构造函数

当您要为参数传递不同的值时,这很有用。示例代码

public class EnvSettings
{
    public bool IsDebug {get; private set;}
    public EnvSettings(bool isDebug)
    {
        IsDebug = isDebug;
    }   
}
// then  elsewhere
public void Foo()
{
    var settings = EnvSettings(false);
    if(settings.IsDebug)
    {
        // this is debug
    }
    else
    {
        // this is something else
    }
}

将值作为参数传递给方法

public class Foo
{   
    public void DoFoo
    {
         bool isDebug = false;
         var bar = new Bar();
         bar.DoBar(isDebug)
    }   
}
public class Bar
{   
    public void DoBar(bool isDebug)
    {
        if(isDebug)
        {
            // this is debug set
        }
        else
        {
            // this is something else
        }
    }   
}