ASP.NET MVC 4 Questions and Answers


Q) What is the name of the unit testing Framework that is available with a ASP.NET MVC 4 Project in Visual Studio?

MSTest

Q) Which code will execute before any of the Controllers are run?

The code within Application_Start() in global.asax will start executing before any of the Controllers run

Q) Which HTTP status code does a RedirectPermanent ActionResult return?

RedirectPermanent returns a 301 status code

Q) How does ASP.NET know how to deliver a request like http://localhost/home/about?

Routing engine (not tied to ASP.NET MVC Framework) directs requests to Controllers. The route map is defined in global.asax

Q) What is NuGet?

It is a Package Manager for .NET applications. NuGet can be used to install, update and configure software (DLLs) in the form of Packages, for use in a Visual Studio project. This way you don't have to download it or track dependencies. NuGet understands package dependencies & will fetch dependent packages as well.

Q) Where do NuGet Packages come from?

1. NuGet official feed of packages (from nuget.org)
2. Your local repository
3. Your network share
Q) Where do NuGet Packages go?

When you install a NuGet package it goes into the Packages folder within the Visual Studio Solution from where you requested. It stores the Package on a per solution basis, not putting in the GAC or Program Files. It is local to that Solution. This makes it easy to update and put a package under version control. The Packages.config file within a Visual Studio solution lists all the Packages that have been installed for the project

Q) What is in a NuGet Package?

A *.nupkg is a zip file that contains:
Metadata - dependencies, URLs, version numbers are specified in a XML file
Binaries - assemblies (DLLs)
Other content - scripts, images, code blocks

Q) Can NuGet commands be executed from the command line?

Yes, using PowerShell. To view the list of commands, open the Package Manager Console in Visual Studio (View > Other Windows > Package Manager Console) & type help package

Q) Which is the namespace you have to include when you're using DataAnnotations?

System.ComponentModel.DataAnnotations

Q) Where does the Razor Layout View reside?

The Layout View in Razor (the equivalent of Master Page in ASP.NET WebForms) resides in the Shared folder under the Views folder. It is represented by the _Layout.cshtml file

Q) How does MVC runtime know that it has to use _Layout.cshtml?  
Q) What is the significance of the _ViewStart.cshtml file?

_ViewStart.cshtml resides in the root of the Views folder. It contains a code block containing a Layout property set to the the _Layout.cshtml file. It is a Razor convention that anything inside  _ViewStart.cshtml will execute before a View does. _ViewStart.cshtml applies to all Views. It can be overriden on a per View basis.

Q) What does a Razor Layout page contain?

Razor Layout pages are HTML files that define a Body and additional sections by using the following instructions:
@RenderBody(): Renders the content of a page that is not within any named sections.
@RenderSection("mySectionName"): Renders the content of the section within the given name. Sections are mandatory by default, so each page linked to the template will have to declare them.
@RenderSection("mySectionName", optional:true): Renders the content of an optional section. The pages that are linked to the template can omit the definition of an optional section.

Q) How to ignore the default Layout view?

To avoid using the default Layout page, set Layout property to null

Q) Can you build your own custom HTML Helpers?

Yes, you can build your custom HTML Helpers

Q) What does the Razor WebGrid Helper do?

The WebGrid Helper renders an HTML table that displays data from the model or from a database. It supports data formatting, paging and sorting.

It generates a collection of Columns and parameters within the given data source, which is rendered as an HTML table that can be accessed after the grid is generated.

Q) What does @: in Razor represent?

@: makes Razor interpret a string as literal text

Q) What are the project templates presented to you by Visual Studio when you start a ASP.NET MVC 4 Project?

Empty - empty ASP.NET MVC 4 Project
Basic - basic ASP.NET MVC 4 Project
Internet Application - a default ASP.NET MVC 4 Project with an account controller that uses Forms Authentication
Intranet Application - a default ASP.NET MVC 4 Project with an account controller that uses Windows Authentication
Mobile Application - an ASP.NET MVC 4 Project for mobile devices with an account controller that uses Forms Authentication
Web API - a ASP.NET Web API Project

Q) What are Action Filters? Give examples.

Action Filters are the components that are used to apply cross-cutting logic i.e logic that has to be executed across multiple controller actions so that you don’t have to duplicate code inside of individual controllers.

Action Filters supply pre and post processing logic to a controller action and its result

Global filters are applied at application startup. 

Examples:
OutputCache - caches the output of a Controller
Authorize - restricts an Action to authorized users or roles
ValidateAntiForgeryToken - helps prevent cross site request forgeries
HandleError - can specify a view to render in the event of an unhandled exception.

work in progress...

Also see:

Comments