簡體   English   中英

在Asp.net Core 2.0中,IHostingEnvironment是可擴展的

[英]In Asp.net Core 2.0 is IHostingEnvironment extendable

當前,Asp.Net核心2 IHostingEnvironment具有三個布爾屬性

  • IsProduction
  • IsStaging
  • IsDevelopment

如果要創建兩個其他屬性,它可以擴展嗎? (例如IsTesting,IsCloudDb)

由於我不是專業程序員,所以我不確定如果可行的話該怎么做。

這些不是屬性,而是IHostingEnvironment接口的擴展方法。 所有這些擴展方法所做的就是將IHostingEnvironment.EnvironmentName與預定義的字符串進行比較。 你也可以做到的:

public static class EnvironmentExtensions {
    const string CloudDbEnvironment = "CloudDb";
    const string TestingEnvironment = "Testing";

    public static bool IsCloudDb(this IHostingEnvironment env) {
        return env.IsEnvironment(CloudDbEnvironment);
    }

    public static bool IsTesting(this IHostingEnvironment env) {
        return env.IsEnvironment(TestingEnvironment);
    }
}

當然,您應該將EnvironmentName設置為相關的字符串,以使這些方法返回true。

暫無
暫無

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

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