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
//Global
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


//Slow
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


//Closure
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?
http://stackoverflow.com/questions/6614572/javascript-performance-with-closure

Why Most Unit Testing is Waste

http://www.rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf

Summary:
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




Reference:
http://en.wikipedia.org/wiki/HTML5_video

Tuesday, April 22, 2014

What is sparse array?

http://en.wikipedia.org/wiki/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:
http://stackoverflow.com/questions/5318385/mvc-3-how-to-render-a-view-without-its-layout-page
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
http://msdn.microsoft.com/en-us/library/aa347733.aspx
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.
https://www.nuget.org/packages/UpgradeMvc3ToMvc4

Then follow instruction from Microsoft to upgrade manually:
http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253806

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=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />

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

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, 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:
C:\Windows\System32\inetsrv\InetMgr.exe

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
        $.ajax({
            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

      
    
Reference:
http://www.iis.net/configreference/system.webserver/staticcontent/clientcache


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
        [HttpGet]
        public ActionResult FacebookLogin(string token)
        {
            WebClient client = new WebClient();
            string JsonResult = client.DownloadString(string.Concat(
                   "https://graph.facebook.com/me?access_token=", 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 https://developers.facebook.com/
->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

http://html2canvas.hertzen.com/screenshots.html

What Javascript Frameworks are used at Twitter.com?

http://vitalflux.com/ui-frameworks-used-twitter-com/

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

http://www.dropzonejs.com/

Tuesday, April 1, 2014

How to Correctly Detect Credit Card Type

http://creditcardjs.com/credit-card-type-detection

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
JCB3528-3589
Laser6304, 6706, 6771, 6709
Maestro5018, 5020, 5038, 5612, 5893, 6304, 6759, 6761, 6762, 6763, 0604, 6390
Dankort5019
MasterCard50-55
Visa4
Visa Electron4026, 417500, 4405, 4508, 4844, 4913, 4917