Thursday, December 14, 2017

C# - How to merge paths with slashes in all subsequent params

To combine file paths with slashes will take care by below code

public static string MergePath(params string[] stringPaths)
        {

            for (int i = 0; i < stringPaths.Length; i++)
            {
                //To skip trim on first path
                if (i != 0)
                    stringPaths[i] = stringPaths[i].Trim().TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
            }

            return Path.Combine(stringPaths);

        }



Example:
var stringtest = Extensions.MergePath(@"c:\path\", @"\dir\file.txt");
var stringtest2 = Extensions.MergePath(@"c:\path\",@"\test\adsf\", @"\dir\file.txt");
var stringtest3 = Extensions.MergePath(@"c:\path", @"test\adsf", @"dir\file.txt");

Output:
"c:\\path\\dir\\file.txt"
"c:\\path\\test\\adsf\\dir\\file.txt"
"c:\\path\\test\\adsf\\dir\\file.txt"

Monday, July 10, 2017

IIS - Application warmup

How to warm up IIS application especially when using Entity framework

Follow below steps to enable your website always respond quickly,

- Check "Application Initialization" feature enabled in windows features turn or off



- Start Mode of Application pool set as "Always Running" in AppPool Advanced Settings
- Preload of Website should be set as "true" in Website advanced settings
- Add Application Initialization script in web.config of your application as below, You can specify valid URL which can initialize all items such as SQL DB, EF etc.,

<system.webServer>
  <applicationInitialization doAppInitAfterRestart="true" skipManagedModules="false">
      <add initializationPage="/YourUrl/YourMethodAnything" />
    </applicationInitialization>
</system.webServer>

Following these steps will solve your idle timeout application responding slow issue.

Tuesday, March 7, 2017

JavaScript - Simple Export to excel

To export excel in JavaScript without any server side techiques, we can use following steps

Used blob file saver for download https://github.com/eligrey/FileSaver.js

Step 1: Create Html Content with required styles as below

var tableHtml = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
        tableHtml += '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets>';
        tableHtml += '<x:ExcelWorksheet><x:Name>PlanFundLineup</x:Name>';
        tableHtml += '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
        tableHtml += '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
  tableHtml += "<div><table  border='1'>" +
        tableHtml += "<thead>" +
        tableHtml += "<tr><th>" +
        tableHtml += "Header Text" +
        tableHtml += "</th></tr>" +
 tableHtml += "</thead>" +
               "<tbody>" +
        tableHtml += "<tr><td>" +
        tableHtml += "Content Text" +
        tableHtml += "</td></tr>" +
               "</tbody>" +
              "</table></div>";
        tableHtml += '</body></html>';

Step 2: Download this html content using blob

var blob = new Blob([tableHtml], { type: "application/vnd.ms-excel;charset=utf-8" })
window.saveAs(blob, "excelname.xls");