天天看點

.net 開發的奇淫巧計

随機數

Random random = new Random(( int)DateTime .Now.Ticks & 0x0000FFFF);      

如何讓ASP.NET Web API顯示完整的錯誤資訊,并顯示錯誤堆棧?

在ASP.NET Web  API項目的Application_Start中添加下面的代碼:

GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

自動清除運作測試用例之後産生的 TestResults垃圾檔案夾

在測試項目的配置檔案的末尾裡添加

<PropertyGroup>

    <TestResultsFolderPath>..\TestResults</TestResultsFolderPath>

</PropertyGroup>

<Target Name="AfterClean">

    <RemoveDir Directories="$(TestResultsFolderPath)" Condition="Exists('$(TestResultsFolderPath)')" />

</Target>
      

 

或者用msbuild的指令

MSBuild /t:Clean MyProject.csproj      

@的用法

1字元串換行

Regular literal Verbatim literal Resulting string

"Hello"

@"Hello"

Hello

"Backslash: \\"

@"Backslash: \"

Backslash: \

"Quote: \""

@"Quote: """

Quote: "

"CRLF:\r\nPost CRLF"

@"CRLF:

Post CRLF"

CRLF:

Post CRLF

 2用語言的保留關鍵字作為變量名(不是很建議這麼做

× object object=null;

√ object @bject=null;

 傳回目前的方法簽名

using System.Reflection;

 [Test]
        public void 傳回目前方法名()
        {
            StackTrace st = new StackTrace();
            StackFrame sf = st.GetFrame(0);
            MethodBase currentMethodName = sf.GetMethod();
            Console.WriteLine(currentMethodName.ToString());
        }

//Void 傳回目前方法名()
      

  

編輯器特性

生成構造函數

構造函數 輸入ctor 在按TAB鍵

public const int

public const string

public int property { get; private set; } propg

public static readonly

web.config

移除X-AspNet-Version HTTP頭

在web.config裡配置

<!-- 移除X-AspNet-Version HTTP頭 -->
    < httpRuntime targetFramework ="4.5 " enableVersionHeader ="false " />      

參考連結:

http://blogs.msdn.com/b/ploeh/archive/2006/07/13/cleaningawaythetestresultsfolder.aspx

繼續閱讀