Need to practice your ASP.NET (Core) MVC skills for an upcoming job interview? Try solving these ASP.NET (Core) MVC interview questions that test knowledge of Razor, Action methods and attributes, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.
These ASP.NET (Core) MVC interview questions are examples of real tasks used by employers to screen candidates for web development jobs that require knowledge of the MVC pattern, ASP.NET and the .NET Framework in order to build robust and scalable web applications.
1. Action Attributes
Which statements are true about action attributes in ASP.NET (Core) MVC?
(Select all acceptable answers.)
2. Passing Data
Select all the methods that can be used to pass data from the controller to the view.
(Select all acceptable answers.)
3. Meal View
Consider the following C# model:
public class Meal
{
public string Name { get; set; }
public double Calories { get; set; }
public List<string> Ingredients { get; set; }
}
The following template for Razor view engine accepts a Meal as model. Fill in the blanks with the simplest variants so that correct properties are printed.
<html>
<body>
<h1>Name: </h1>
<p>Calories: </p>
<p>Ingredients:</p>
<ul>
@foreach (var ingredient in )
{
<li></li>
}
</ul>
</body>
</html>
4. User Controller
Add appropriate routing attributes to the GetUsers and GetUserById actions so that:
- GetUsers should activate on URL "/users".
- GetUserById should activate on URLs "/users/userId" where userId is a string that represents the id of the user.
- The GetUser action is correctly routed: Wrong answer
- The GetUserById action is correctly mapped: Wrong answer
5. Address Controller
Consider the following view, provided here and below, which is placed in Views/Address/Create.cshtml:
@model Address
<!DOCTYPE html>
<html>
<head>
<title>Create a new address</title>
</head>
<body>
<form method="post" action="/user/address/save">
<label asp-for="Street">Street:</label>
<input type="text" asp-for="Street" />
<label asp-for="City">City:</label>
<input type="text" asp-for="City" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Add actions to the AddressController so that:
- On requests to "user/address/create", the view above should be returned.
- On submitting the form from the view above, AddressController should add the Address that was submitted into the AddressController.addresses field and then redirect to the "user/address/create" URL.
Actions should use attribute routing.
- /user/address/create and /user/address/save URLs are mapped to distinct actions: Wrong answer
- /user/address/create URL returns expected view: Wrong answer
- Address is added to the AddressController.addresses list when the form is submitted: Wrong answer
- After saving the address, user is redirected to /user/address/create URL: Wrong answer