Wednesday, April 30, 2014

How to sovle problem, JavaScript runtime error: Object doesn't support property or method 'on'

To me,
I fixed this problem by using higher version jQuery
Originally, it was 1.5.3, changed to 1.8.3

Code snippet for reading configuration from config file

System.Configuration.ConfigurationManager.AppSettings["SettingKey"] == null ? "defaultValue" : System.Configuration.ConfigurationManager.AppSettings["SettingKey"].ToString();

Wednesday, April 23, 2014

Why need Closure in JavaScript

Safe and faster
var names = ['zero', 'one', 'two',  'three', 'four', 'five', 'six',  'seven', 'eight', 'nine'];
var digit_name = function (n) {
 return names[n];
alert(digit_name(3)); // 'three'

//Problem: not safe. It is global variable, other code could change it

var digit_name = function (n) { 
 var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; 
 return names[n];
alert(digit_name(3)); // 'three'
//Problem: Safe but slow. Names hide in function scope, but need to initialize every time

var digit_name = (function () {
 var names = ['zero', 'one', 'two',  'three', 'four', 'five', 'six',  'seven', 'eight', 'nine'];
 return function (n) {
 return names[n];
alert(digit_name(3)); // 'three
//Safe and fast 

But why closure version code is faster?

Why Most Unit Testing is Waste

Keep regression tests around for up to a year — but most of those will be system-level tests rather than unit tests.
• Keep unit tests that test key algorithms for which there is a broad, formal, independent oracle of correctness, and for which there is ascribable business value.
• Except for the preceding case, if X has business value and you can text X with either a system test or a unit test, use a system test — context is everything.
• Design a test with more care than you design the code.
• Turn most unit tests into assertions.
• Throw away tests that haven’t failed in a year.
• Testing can’t replace good development: a high test failure rate suggests you should shorten development intervals, perhaps radically, and make sure your architecture and design regimens have teeth
• If you find that individual functions being tested are trivial, double-check the way you incentivize developers’ performance. Rewarding coverage or other meaningless metrics can lead to rapid architecture decay.
• Be humble about what tests can achieve. Tests don’t improve quality: developers do.

Play video in Html 5


Tuesday, April 22, 2014

What is sparse array?
a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

How to render an action without layout in ASP.NET MVC?

    public class HomeController : Controller
        public string Create()
     return "Hello";
There are a couple of other ways:
Change Layout default setting etc.
But return a string or json result, it is great way in some scenarios.

Thursday, April 17, 2014

Wednesday, April 16, 2014

How to solve problem: Proxy class not generated by wsdl command line or add service reference?

Use svcutil command line
svcutil  yourwsdlfile.wsdl
ServiceModel Metadata Utility Tool is a newer version than wsdl

Tuesday, April 15, 2014

How to manually update ASP.NET MVC 3 project to ASP.NET MVC 4?

Tried  nugget first, failed. If a simple project, should try this first.

Then follow instruction from Microsoft to upgrade manually:

In addition to reference above:
1. Change web.config file in view folder  as well
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

2. Add reference to System.Web.Optimization
By run nuget package: Install-Package Microsoft.AspNet.Web.Optimization

How to create a Windows EventLog source by command line?

Open command line window with admin permission
eventcreate /ID 2 /L APPLICATION /T INFORMATION /SO YourEventSourceName /D "Application infor"

Thursday, April 10, 2014

Where to check IIS server version

Check file property:

Tuesday, April 8, 2014

How to prevent cache in ASP.NET Single Page Application when use Javascript Ajax calls?

Turn off cache in Jquery Ajax call
            url: "url",
            cache: false,
            dataType: 'json',
            async: true,
            success: function (data) {
            error: function (xhr, ajaxOptions, thrownError) {

How to solve ASP.NET precompile problem, The type or namespace name does not exist in the namespace 'System.Web.Mvc' (are you missing an assembly reference?)

Set Copy local to true for this project reference: System.Web.Mvc
And Clear solution

How to enable static content caching in ASP.NET?

Add following configuration into web.config file


When run pagespeed, sometime will get following warning:
Leverage browser caching
Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over

Enable ASP.NET static content cache to improve speed

Monday, April 7, 2014

Where to get ASP.NET MVC project MVC version?

Project reference -> system.web.mvc -> Properties- > Version

Wednesday, April 2, 2014

How to add Facebook login function into ASP.NET MVC project?

Step 1: create a Facebook app, and get appid
Step 2: Add following code into login view file

Step 3: Create an action in AccountController
        public ActionResult FacebookLogin(string token)
            WebClient client = new WebClient();
            string JsonResult = client.DownloadString(string.Concat(
                   "", token));

            var serializer = new JavaScriptSerializer();
            dynamic value = serializer.DeserializeObject(JsonResult);
            var model = new RegisterModel()
                Email = value["email"],
                FirstName = value["first_name"],
                LastName = value["last_name"]

            FormsAuthentication.SetAuthCookie(model.Email, true /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");

How to solve problem, Given URL is not allowed by the Application configuration Facebook application error?

Go to
->Your app ->Settings -> Addvanced -> Valid OAuth redirect URIs -> put your login url in here

How to create a web page screenshot by Javascript lib, html2canvas

What Javascript Frameworks are used at

DropzoneJS, an open source library that provides drag'n'drop file uploads with image previews

Tuesday, April 1, 2014

How to Correctly Detect Credit Card Type

Card Type
Card Number Prefix
American Express34, 37
China UnionPay62, 88
Diners ClubCarte Blanche300-305
Diners Club International300-305, 309, 36, 38-39
Diners Club US & Canada54, 55
Discover Card6011, 622126-622925, 644-649, 65
Laser6304, 6706, 6771, 6709
Maestro5018, 5020, 5038, 5612, 5893, 6304, 6759, 6761, 6762, 6763, 0604, 6390
Visa Electron4026, 417500, 4405, 4508, 4844, 4913, 4917