Monday, April 20, 2015

Asp.net Question and answer for 2 yr Experience


Asp.net Question and answer for 2 yr Experience

Describe state management in ASP.NET.

State management is a technique to manage a state of an object on different request.
The HTTP protocol is the fundamental protocol of the World Wide Web. HTTP is a stateless protocol means every request is from new user with respect to web server. HTTP protocol does not provide you with any method of determining whether any two requests are made by the same person.
Maintaining state is important in any web application. There are two types of state management system in ASP.NET.
- Client-side state management
- Server-side state management

Explain client side state management system.
ASP.NET provides several techniques for storing state information on the client. These include the following:
- view state ASP.NET uses view state to track values in controls between page requests. It works within the page only. You cannot use view state value in next page.

- control state: You can persist information about a control that is not part of the view state. If view state is disabled for a control or the page, the control state will still work.

- hidden fields: It stores data without displaying that control and data to the user’s browser. This data is presented back to the server and is available when the form is processed. Hidden fields data is available within the page only (page-scoped data).

- Cookies:Cookies are small piece of information that server creates on the browser. Cookies store a value in the user’s browser that the browser sends with every page request to the web server.

- Query strings: In query strings, values are stored at the end of the URL. These values are visible to the user through his or her browser’s address bar. Query strings are not secure. You should not send secret information through the query string.

Explain server side state management system.
The following objects are used to store the information on the server:
- Application State:
This object stores the data that is accessible to all pages in a given Web application. The Application object contains global variables for your ASP.NET application.
- Cache Object: Caching is the process of storing data that is used frequently by the user. Caching increases your application’s performance, scalability, and availability. You can catch the data on the server or client.
- Session State: Session object stores user-specific data between individual requests. This object is same as application object but it stores the data about particular user.

Explain cookies with example.
A cookie is a small amount of data that server creates on the client. When a web server creates a cookie, an additional HTTP header is sent to the browser when a page is served to the browser. The HTTP header looks like this:
Set-Cookie: message=Hello. After a cookie has been created on a browser, whenever the browser requests a page from the same application in the future, the browser sends a header that looks like this:
Cookie: message=Hello
Cookie is little bit of text information. You can store only string values when using a cookie. There are two types of cookies:
- Session cookies
- Persistent cookies.
A session cookie exists only in memory. If a user closes the web browser, the session cookie delete permanently.

A persistent cookie, on the other hand, can available for months or even years. When you create a persistent cookie, the cookie is stored permanently by the user’s browser on the user’s computer.

Creating cookie
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Cookies[“message”].Value = txtMsgCookie.Text;
}
// Here txtMsgCookie is the ID of TextBox.
// cookie names are case sensitive. Cookie named message is different from setting a cookie named Message.

The above example creates a session cookie. The cookie disappears when you close your web browser. If you want to create a persistent cookie, then you need to specify an expiration date for the cookie.
Response.Cookies[“message”].Expires = DateTime.Now.AddYears(1);
Reading Cookies
void Page_Load()
{
if (Request.Cookies[“message”] != null)
lblCookieValue.Text = Request.Cookies[“message”].Value;
}

// Here lblCookieValue is the ID of Label Control.
Describe the disadvantage of cookies.
- Cookie can store only string value.
- Cookies are browser dependent.
- Cookies are not secure.
- Cookies can store small amount of data.


No comments:

Post a Comment