【问题标题】:Web API 2 accessing ServiceController : ApiController public variable from classWeb API 2 访问 ServiceController:类中的 ApiController 公共变量
【发布时间】:2016-05-23 22:46:04
【问题描述】:

嘿,我想弄清楚如何从 ServiceController 访问变量:ApiController 像这样:

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

我想要获得的是 isDebuging 值,但在我的 Class 文件 中:

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

这可能吗?为了从 ServiceController : ApiController 获取或设置值,我似乎找不到正确的语法。

任何帮助都会很棒!

【问题讨论】:

  • 确保代码不会远程工作 :) 您在哪里/如何设置 isDebugging?
  • @ClaudioRedi 只是普通的 isDebugging = false; 如果我在 ServiceController : ApiController 类中,它工作得很好。但是,一旦我在另一个创建的类文件中并尝试引用该公共变量,那么这就是我想做的事情。
  • 阅读更多关于如何在 C# 中创建类实例dotnetperls.com/new
  • 啊,@Regfor。谢谢!随意将其作为官方答案。

标签: c# variables asp.net-web-api asp.net-web-api2 public-method


【解决方案1】:

那个检查环境应该是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
...

...所谓的过滤方法将在整个 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 的控制器。

【讨论】:

    【解决方案2】:

    isDebugging 属性设为静态可能有助于ServiceController.isDebugging = true;,但简单的问题是您为什么需要它。如果你需要一个全局属性,你可以使用 Session。

    【讨论】:

      【解决方案3】:

      你可能做错了。这几个选择应该让你开始。最后两个选项非常适合单元测试。

      #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
              }
          }   
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 2012-04-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多