0% found this document useful (0 votes)
13 views6 pages

ASPNET Program Based 2

The document provides a series of questions and answers related to ASP.NET coding practices, specifically focusing on cookie management, Razor Pages, MVC architecture, session handling, and user authentication. Each question includes a missing line of code that is essential for the functionality described, along with the correct answer. The document serves as a reference for developers to understand common coding patterns in ASP.NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

ASPNET Program Based 2

The document provides a series of questions and answers related to ASP.NET coding practices, specifically focusing on cookie management, Razor Pages, MVC architecture, session handling, and user authentication. Each question includes a missing line of code that is essential for the functionality described, along with the correct answer. The document serves as a reference for developers to understand common coding patterns in ASP.NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is the missing line of code to set a cookie in ASP.NET?

HttpCookie cookie = new HttpCookie("UserName", "John");

// Missing line

Response.Cookies.Add(cookie);

• A) cookie.Expires = DateTime.Now.AddHours(1);
• B) cookie.Path = "/";
• C) cookie.Secure = true;
• D) cookie.HttpOnly = true;
• Answer: A) cookie.Expires = DateTime.Now.AddHours(1);

What is the missing line of code to read a cookie in ASP.NET?

HttpCookie cookie = Request.Cookies["UserName"];

// Missing line

• A) string userName = cookie.Value;


• B) cookie.Expires = DateTime.Now.AddHours(1);
• C) cookie.Path = "/";
• D) cookie.Secure = true;
• Answer: A) string userName = cookie.Value;

What is the missing line of code to delete a cookie in ASP.NET?

HttpCookie cookie = Request.Cookies["UserName"];

// Missing line

Response.Cookies.Add(cookie);

• cookie.Expires = DateTime.Now.AddDays(-1);
• B) cookie.Path = "/";
• C) cookie.Secure = true;
• D) cookie.HttpOnly = true;
• Answer: A) cookie.Expires = DateTime.Now.AddDays(-1);
What is the missing line of code to check if a cookie exists in ASP.NET?

// Missing line

if (cookie != null)

// Cookie exists, proceed with operations

• HttpCookie cookie = Request.Cookies["UserName"];


• B) cookie.Expires = DateTime.Now.AddHours(1);
• C) cookie.Path = "/";
• D) cookie.Secure = true;
• Answer: A) HttpCookie cookie = Request.Cookies["UserName"];

What is the missing line of code to set a secure flag for a cookie in ASP.NET?

HttpCookie cookie = new HttpCookie("AuthToken", token);

// Missing line

Response.Cookies.Add(cookie);

• A) cookie.Expires = DateTime.Now.AddHours(1);
• B) cookie.Path = "/";
• C) cookie.Secure = true;
• D) cookie.HttpOnly = true;
• Answer: C) cookie.Secure = true;

What is the missing line of code to bind a form field to a model property in a Razor Page in ASP.NET?

<form method="post">

<input type="text" id="name" name="name" />

// Missing line

<button type="submit">Submit</button>
</form>

• A) asp-for="name"
• B) asp-route="name"
• C) asp-validation-for="name"
• D) asp-items="name"
• Answer: A) asp-for="name"

What is the missing line of code to display a model property value in a Razor Page in ASP.NET?

<h1>Message: @Model.Message</h1>

// Missing line

• A) @{ ViewData["Message"] = Model.Message; }
• B) @{ ViewBag.Message = Model.Message; }
• C) @{ TempData["Message"] = Model.Message; }
• D) @{ Html.DisplayFor(m => m.Message); }
• Answer: A) @{ ViewData["Message"] = Model.Message; }

What does MVC stand for in ASP.NET MVC?


• A) Model View Controller
• B) Multiple View Components
• C) Model-View-Component
• D) Multilayered View Control
• Answer: A) Model View Controller

Which component in MVC is responsible for interacting with the database and
managing application data?
• A) Model
• B) View
• C) Controller
• D) ViewModel
• Answer: A) Model

In ASP.NET MVC, which file is responsible for handling incoming requests,


processing user input, and returning the appropriate view to the user?
• A) Model.cs
• B) View.cshtml
• C) Controller.cs
• D) ViewModel.cs
• Answer: C) Controller.cs

What is the missing line of code to redirect the user after successful login
authentication in an ASP.NET Web Forms login page?

protected void LoginButton_Click(object sender, EventArgs e)

if (IsValid)

// Missing line

• A) Response.Redirect("~/Home.aspx");
• B) Server.Transfer("~/Home.aspx");
• C) Response.RedirectToRoute("Home");
• D) Server.TransferRequest("~/Home.aspx");
• Answer: A) Response.Redirect("~/Home.aspx");

What is the purpose of the Session["username"] variable in this ASP.NET login


program?
• A) To store the user's password securely
• B) To store the user's username for later use
• C) To check whether the user is logged in or not
• D) To store the user's session ID
• Answer: B) To store the user's username for later use

What happens when a user successfully logs in using the provided ASP.NET
login program?
• A) The user's username and password are stored in a database
• B) The user is redirected to a specific homepage based on their credentials
• C) The user's session ID is invalidated
• D) The user's password is encrypted and stored in a cookie
• Answer: B) The user is redirected to a specific homepage based on their
credentials
What does the Session["username"] != null condition check for in the provided
ASP.NET login program?
• A) Whether the user's session has expired
• B) Whether the user's username is not empty
• C) Whether the user is currently logged in
• D) Whether the user's password is correct
• Answer: C) Whether the user is currently logged in

What is the missing line of code to clear the session after a user logs out?

protected void Logout_Click(object sender, EventArgs e)

// Missing line

Response.Redirect("LoginPage.aspx"); // Redirect to the login page

• A) Session.Clear();
• B) Session.Abandon();
• C) Session.Remove("username");
• D) Session.RemoveAll();
• Answer: B) Session.Abandon();

What is the missing line of code to prevent unauthorized access to the user's
homepage by directly entering the URL without logging in?

protected void Page_Load(object sender, EventArgs e)

// Missing line

if (Session["username"] == null)

Response.Redirect("LoginPage.aspx"); // Redirect to the login page

}
}

• A) CheckUserAuthorization();
• B) CheckLoggedIn();
• C) CheckSession();
• D) CheckLoginStatus();
• Answer: C) CheckSession();

You might also like