Web API 2访问ServiceController:来自类的ApiController公共变量

时间:2016-02-12 17:13:42

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

嘿所有我试图弄清楚如何从ServiceController访问变量: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...

这可能吗?我无法找到正确的语法,以便从 ServiceController:ApiController 中获取或设置值。

任何帮助都会很棒!

3 个答案:

答案 0 :(得分:2)

检查环境应该是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控制器执行任何操作之前,将触发所谓的过滤方法。

BTW,我会按如下方式设计一个接口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的控制器。

答案 1 :(得分:1)

将属性isDebugging设为静态可能对ServiceController.isDebugging = true;有所帮助,但接下来的简单问题就是为什么需要这样做。如果您需要全局属性,则可以使用Session。

答案 2 :(得分:0)

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

#ifdef DEBUG

如果您想要一些只在调试版中可见的调试代码,您可以使用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
        }
    }   
}
相关问题