Asp.Net
Top 50 Asp.Net Web API Interview Questions and Answers
Download PDF 1) What is Web API? WebAPI is a framework which helps you to build/develop HTTP...
The HTTP protocol on which all web applications work is a stateless protocol. By stateless, it just means that information is not retained from one request to another.
For instance, if you had a login page which has 2 textboxes, one for the name and the other for the password. When you click the Login button on that page, the application needs to ensure that the username and password get passed onto the next page.
In ASP.Net, this is done in a variety of ways. The first way is via a concept called ViewState. This is wherein ASP.Net automatically stores the contents of all the controls. It also ensures this is passed onto the next page. This is done via a property called the ViewState.
It is not ideal for a developer to change anything in the view state. This is because it should be handled by ASP.Net only.
The other way is to use an object called a "Session Object." The Session object is available throughout the lifecycle of the application. You can store any number of key-value pairs in the Session object. So on any page, you can store a value in the Session object via the below line of code.
Session["Key"]=value
This stores the value in a Session object and the 'key' part is used to give the value a name. This allows the value to be retrieved at a later point in time. To retrieve a value, you can simply issue the below statement.
Session["Key"]
In our example, we are going to use the Session object to store the name entered in the name textbox field in the page. We are then going to retrieve that value and display it on the page accordingly. Let's add the below code to the Demo.aspx.cs file.
protected void btnSubmit_Click(object sender,EventArgs e)
{
Session["Name"] = txtName.Text;
Response.Write(Session["Name"]);
lblName.Visible = false;
txtName.Visible = false;
1stLocation.Visible = false;
chkC.Visible = false;
chkASP.Visible = false;
rdMale.Visible = false;
rdFemale.Visible = false;
btnSubmit.Visible = false;
}Code Explanation:-
Once you make the above changes, you will see the following output
Output:
From the output, you can see that the Session value of name was retrieved and displayed in the browser.
Summary:
Download PDF 1) What is Web API? WebAPI is a framework which helps you to build/develop HTTP...
In any application, errors are bound to occur during the development process. It is important to...
This is a curated list of most frequently asked .NET Interview Questions and Answers that help...
Let's look at an example of how we can implement a simple "hello world" application. For this, we...
What is WCF? WCF stands for Windows Communication Foundation. It is used to create a distributed and...
What is ASP.NET MVC? ASP.NET MVC is an open source web development framework from Microsoft that...