Pages

Saturday, April 26, 2014

Lifecycle of an Asp.NET MVC 5 Application

lifecycle-of-an-aspnet-mvc-5-application
lifecycle-of-an-aspnet-mvc-5-application2
Lifecycle of an Asp.NET MVC 5 Application
This post is for the purpose of my notes only.
“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

JavaScript and Knockout are not Synonymous and Data- Abuse

Example: What is the difference?


Knockout's click:
<button data-bind="click: resetTicket">Clear</button>


Old-Style DOM onclick:
<input type="button" onclick="resetTicket" />


Knockout polluting the Html DOM.


Within the UI window, a lot of support had been placed toward Separation of Concerns. Separating styling and behavior from the DOM. Moving CSS and JavaScript into distinct external files. Moving JavaScript's onclick handler from being DOM inline to being placed into an external JavaScript file.

Surprising, Knockout is gaining big following, but, Knockout has moved JavaScript behavior back into the DOM and they are abusing the data- attribute.


Knockout isn’t the only JavaScript library polluting the DOM. There are many libraries and frameworks polluting a page's DOM and abusing the data- attribute.


I can think one possible reason why Knockout and other libraries are becoming popular, because there still exist a hatred toward JavaScript, however “developers” want non-spaghetti JavaScript code, yet they do not want to fully learn JavaScript.


I don't know if I'm the only one concerned? I believe in time, there will be a movement advocating less corruption of data- attributes and a support for removing this behavior from the DOM.


I don't support KnockoutJS and it is funny, the last job I interviewed for asked if I knew JavaScript. However what they were really asking, if I knew KnockoutJS.


Support Native JavaScript.
This post is for the purpose of my notes only. 
“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

Saturday, April 19, 2014

ASP.Net Web API 2 Setup for MVC 4

Quick note adding Web API to existing application. Within Visual Studio, I’m starting from an empty MVC 4.0 application.

1. Add references:

  • NewtonSoft.Json
  • System.Net.Http
  • System.Net.Formatting
  • System.Web.Http
  • System.Web.Http.WebHost

PM> Install-Package Microsoft.AspNet.WebApi.WebHost
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (≥ 5.1.2 && < 5.2.0)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (≥ 5.1.2)'.
Attempting to resolve dependency 'Newtonsoft.Json (≥ 4.5.11)'.
Installing 'Microsoft.AspNet.WebApi.WebHost 5.1.2'.
Successfully installed 'Microsoft.AspNet.WebApi.WebHost 5.1.2'.
Adding 'Microsoft.AspNet.WebApi.Client 5.1.2' to .
Successfully added 'Microsoft.AspNet.WebApi.Client 5.1.2' to .
Adding 'Microsoft.AspNet.WebApi.Core 5.1.2' to .
Successfully added 'Microsoft.AspNet.WebApi.Core 5.1.2' to .
Adding 'Microsoft.AspNet.WebApi.WebHost 5.1.2' to .
Successfully added 'Microsoft.AspNet.WebApi.WebHost 5.1.2' to .

2. Within App_Start folder add WebApiConfig class:

public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {
id = RouteParameter.Optional
});
}
}


3. Add to Global.asax.cs file:


using System.Web.Http;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}



**Note: Must register WebApiConfig before RouteTable. Registering RouteTable before WebApiConfig will throw 404.


This post is for the purpose of my notes only.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford