簡體   English   中英

Web API 2從類訪問ServiceController:ApiController公共變量

[英]Web API 2 accessing ServiceController : ApiController public variable from class

嘿,我正在嘗試找出如何從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獲取或設置值。

任何幫助將是巨大的!

檢查環境應為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;
          }
     }
}

現在,將整個filter屬性作為常規C#屬性添加到ServiceController

[CheckEnvironmentFilter]
public class ServiceController : ApiController
...

...並且在從整個API控制器執行任何操作之前,將調用所謂的filter方法。

順便說一句,我將設計一個接口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; 但接下來的簡單問題是,您為什么需要它。 如果需要全局屬性,則可以使用Session。

您可能做錯了。 這幾種選擇應該可以幫助您入門。 最后兩個選項非常適合單元測試。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM