Wednesday, August 5, 2015

C# - Test - Accessing Web.config in Test Context

When having for instance a unit test in one project and the need to get settings from web.config in another project. We can actually copy the web.config when building the unit test project to retrieve the properties like this. Insert this in Build Events in project settings.
 copy "$(SolutionDir)\Somesite.Web\Web.config" "$(ProjectDir)$(OutDir)$(TargetFileName).config"   

Saturday, July 4, 2015

Visual Studio - Resharper Code Snippet for Method


It have bugged med a lot lately that there is no default method template snippet in Visual studio.
How this can even be is beyond me, but I found a neat solution:


In Template Manager in Resharper you can easily add this method yourself. Here:



Click create New Template in the Templates Explorer.
This will add a strange file in the background that you can edit and it has No Name as default this name is to be replaced with your snippet name. So just fill in the Shortcut: box to the right.
And add the code into the template and save.


This is the code for a simple method snippet with changeable return type:



After this just go ahead and save and restart visual studio.
Now within a class just type method to write the method head and then go to the method body.

Saturday, June 27, 2015

Friday, June 26, 2015

Resharper - Surround code with etc.

https://www.jetbrains.com/resharper/documentation/help20/Templates/SurroundWith/predefSurroundWith.html

Ctrl + Alt + J

C# - Get Web.config Variable Value


Example value in web.config:

 <appSettings>  
    <add key="ClientId" value="123"/>  
    <add key ="webUrl" value="http://www.nasa.gov/"/>  
  </appSettings>  

C# Get:

 using System.Configuration;  
 string ClientId=ConfigurationManager.AppSettings["ClientId"] 
 string webUrl=ConfigurationManager.AppSettings["webUrl"] 

Jadda jadda null check on string ClientId and webUrl.

Monday, June 8, 2015

C# - Stripping all html from string fast!

Code example:


 /// <summary>  
     /// Compiled regular expression for performance.  
     /// </summary>  
     static Regex _htmlRegex = new Regex(@"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", RegexOptions.Compiled);  
Alternavtive regex = static Regex _htmlRegex = new Regex(@"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", RegexOptions.Compiled);

     /// <summary>  
     /// Remove HTML from string with compiled Regex.  
     /// </summary>  
     public static string StripTagsRegexCompiled(string source)  
     {  
       return _htmlRegex.Replace(source, string.Empty);  
     }