/*****by Jiangong SUN*****/
PART I : ASP.NET
Update 30/05/2013, 09/07/2013
1) What are the differences among Session, Cookie, Cache and ViewState? When do you use them ?
Answer:
Session is stored in Server side. Session could add the server load, it will lose data when page is closed, it's safe.
Get session:
HttpContext.Current.Session[key];
Add session:
HttpContext.Current.Session.Add(key, value);
Set session:
HttpContext.Current.Session[key] = value;
Remove session:
HttpContext.Current.Session.Remove(key);
Cookie is stored in Client side. Cookie can be cheated so it's not safe.
Cookie can maintain user's data in client side for preventing input login information. Session can authenticate users in the server side.
Usage: use Response for writing cookie, and use request for reading cookie.
Get cookie:
HttpContext.Current.Request.Cookies[name];
Add cookie:
HttpCookie aCookie = new HttpCookie(COOKIE_NAME);
aCookie.Values[COOKIE_LANGUAGE] = cultureCode;
aCookie.Expires = DateTime.Now.AddYears(10);
HttpContext.Current.Response.Cookies.Add(aCookie);
Remove cookie:
No direct code to do that, you can save cookie with a passed time.
aCookie.Expires = DateTime.Now.AddMinutes(-1);
If objects are shareable between user sessions, then use cache. It can prevent loading the resources repeatedly in a short time.
ViewState is available in a single page, once the page is submitted it will be cleared. It can store hidden input and persist control's state after postbacks.
Two caching mechanisms are: application caching, page output caching.
application caching saves data in server memory and require extensive server resources to create. It can prevent of recreating information.
page output caching stores the generated output of pages, controls, and HTTP responses in memory.
Get cache:
HttpContext.Current.Cache[key];
Add cache:
HttpContext.Current.Cache.Add(key.ToString(), cachedItem, null, DateTime.Now.AddMinutes(CST_CACHE_DURATION), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
or use insert:
HttpContext.Current.Cache.Insert("DSN", connectionString);
Set cache :
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
NoCache Specifies that the device making the request should get the response from the Web server each time.
Public Allows the response to be cached by clients and shared (proxy) caches.
Private Specifies that the response is cacheable only on the client and not by shared (proxy server) caches.
Server Specifies that the response is cached only at the origin server.
ServerAndNoCache Applies the settings of both Server and NoCache to indicate that the content is cached at the server but all others are explicitly denied the ability to cache the response.
ServerAndPrivate Specifies that a response should be cached only on the origin server and on the requesting client; proxy servers are not allowed to cache the response.
Remove cache:
HttpContext.Current.Cache.Remove(key.ToString());
Session vs Cookie: Session is stored in server; Cookie is stored in client.
Session vs Cache: Session is for per user; Cache is for all users
Session vs ViewState: Session is for application; ViewState is for one specific page.
2) What is asp.net page life cycle ?
Answer:
SILVER: Start, Init, Load, Validation, Event, Render, Unload
3) What is the difference between HttpHandler and HttpModule ?
Answer:
When a request come to asp.net application, it's firstly processed by multiple modules(authentication module, session module), then by a single handler, and then flow back to modules.
Custom modules must implement the System.Web.IHttpModule interface.
Custom handlers must implement the System.Web.IHttpHandler interface.
Modules are used in application level
Handlers are used in page level
reference: http://geekswithblogs.net//flanakin/articles/ModuleHandlerIntro.aspx
4) QueryString?
QueryString is a collection for retrieve the information from HTTP query string.
Example:
http://jiangong.com?firstname=jiangong&lastname=sun
string firstName = Request.QueryString("firstname");
5) Web control vs. User control
UserControl: A custom control, ending in .ascx, that is composed of other web controls. Its almost like a small version of an aspx webpage. It consists of a UI (the ascx) and codebehind. Cannot be reused in other projects by referencing a DLL.
WebControl: A control hosted on a webpage or in a UserControl. It consists of one or more classes, working in tandem, and is hosted on an aspx page or in a UserControl. WebControls don't have a UI "page" and must render their content directly. They can be reused in other applications by referencing their DLLs.
reference:
http://stackoverflow.com/questions/71092/what-is-the-difference-between-usercontrol-webcontrol-renderedcontrol-and-comp
5) What is the difference between ASP.NET MVC1, MVC2, MVC3, MVC4?
MVC2:
HTML element helper like Html.TextBoxFor(), Html.TextAreaFor() in addition to Html.TextBox() and Html.TextAreaFor()
Support for rendering sub-sections of a page/site using Html.RenderAction
MVC3:
Razor view engine in addition to ASPX view engine
Unobtrusive Ajax and unobtrusive client side validation
<form action="/ajax/callback" id="form0" method="post" οnclick="Sys.Mvc.AsyncForm.handleClick(this,new Sys.UI.DomEvent(event));"
οnsubmit="Sys.Mvc.AsyncForm.handleSubmit(this,new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace,loadingElementId: 'loading', updateTargetId: 'updateme' });">
<form action="/ajax/callback" data-ajax="true" data-ajax-loading="#loading" data-ajax-mode="replace" data-ajax-update="#updateme" method="post">
MVC4:
Use the HTML5 viewport attribute and CSS media queries to improve the display on mobile devices
Use jQuery Mobile for progressive enhancements and for building touch-optimized web UI
Create asynchronous controllers using task support
New template for desktop and mobile
Authentication OpenID, OAuth
Separation of controllers into different folders
Default IIS Express for applications
Different display for different user agent, ex: Iphone, Ipad etc
Bundling: combine multiple files into one
Integrate multiple files to App_Start\BundleConfig.cs
Minification: Delete useless space of files
6) What's the difference between ASP.NET and ASP.NET MVC?
ASP.NET:
ViewState
ASP.NET MVC:
Unit test
Model-view-controller
Stateless