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);  
     }  

Wednesday, May 27, 2015

EPiServer 7.5 - EPiServer.UI.Admin.SiteMgmt Missing

In EPiServer 7.5, the namespace EPiServer.UI.Admin.SiteMgmt has been removed and site & host management now being implemented with SiteDefinitionRepository and SiteDefinition.Current

Example Code:

In EPiServer < 7.1


Get sites

 var sites = SiteInformationHandler.GetSitesInformation(true);  

Get Host:

 var hosts = SiteInformationHandler.GetSiteInformation(this.SiteID, true);  

In EPiServer > 7.5

Get sites

 var siteDefinitionRepository = ServiceLocator.Current.GetInstance<SiteDefinitionRepository>();  
 var sites = siteDefinitionRepository.List().ToList();  

Get Host:

 var hosts = SiteDefinition.Current;