Asp.Net MVC 5 Interview Questions and Answers | Part-2

Nitish Kaushik
WebGentle
Published in
6 min readOct 31, 2019
MVC Interview Questions

Click here to view part 1 of this series. Here is part 2:

16. What is a controller in Asp.Net MVC?

The controller is one of the main pillars of MVC. It is a .cs file that is driven from the Controller class.

17. What is the use of the Controller class?

A controller plays an important role in the application flow by handling incoming requests and providing data to view or to client requests.

18. How to add a new controller in MVC?

You can add a controller using the following steps:

  • Open solution explorer
  • Right-click on Controllers folder (Either in the root folder or under the Area folder)
  • Choose Add
  • And, click on Controller…
Add a new controller
  • A new window will open.
  • Now choose the MVC5 or Web API controller as per the need and click Add.
  • Enter your controller's name and make sure not to remove the Controller suffix from the name.
Add controller name
  • Click on the Add button.
  • That's it. The new controller will be added under the Controllers folder.

19. Why do we need controller suffix in each controller's name?

Asp.Net MVC framework considers a class as a Controller if it has a Controller suffix in its name. But, when using this controller from other places (example: ActionLink) then the Controller suffix is not required.

Here is some code from the Asp.Net MVC GitHub repository.

MVC Application code

You can view this code by clicking here.

“As per this code, Asp.Net MVC Framework is looking for Controller suffix and is using the SubString method to find the name of the controller.”

20. What is the parent class of a controller?

All controllers are driven from the Controller class.

21. What is an Action method?

All public methods available in a controller class are known as an Action method.

22. What are the different return types of Action methods?

In Asp.Net MVC, following return types are available for an action method:

  1. ActionResult: This is the parent class of all other return types. Hence, this can be used to return any other type.
  2. ContentResult: Returns a user defined content type.
  3. EmptyResult: This is used to return nothing. Means if a controller’s action method does not return anything then we use EmptyResult.
  4. FileContentResult: Returns the binary content of a file.
  5. FilePathResult: Returns the content of the file.
  6. FileStreamResult: Sends binary content to the response by using a System.IO.Stream instance.
  7. FileResult: FileResult is the base class for FileContentResult, FilePathResult, FileStreamResult. And is used to send binary of a file to the response.
  8. HttpNotFoundResult: Defines an object that is used to indicate that the requested resource was not found.
  9. HttpStatusCodeResult: Provides a way to return an action result with a specific HTTP response status code and description.
  10. HttpUnauthorizedResult: Represents the result of an unauthorized HTTP request.
  11. JavaScriptResult: Sends JavaScript content to the response.
  12. JsonResult: Sends JSON-formatted content to the response
  13. ViewResult: ViewResult is used to render a View by using System.Web.MVC.IView instance that is returned by an System.Web.Mvc.IViewEngine object.
  14. PartialViewResult: This class is used to send a Partial view to the response.
  15. RedirectResult: Controls the processing of application actions by redirecting to a specified URI.

23. Can we return any other type (string, int, etc.) from Action methods?

Yes, We can return any other type available in .Net from the controller.

24. How to call Action method from your browser?

In Asp.Net MVC we can call an action method with the help of routing.
The default routing for MVC is

http://domain.com/Controller/ActionMethod

Here ActionMethod is the name of to be called action method name and
The controller is the name of the action method’s parent controller name.

25. What is a View in Asp.Net MVC?

View in Asp.Net MVC is a special class that contains pure HTML and data to produce a response for the client.
The view receives data from the controller.

26. What is the extension of the View file?

Extension of a View file depends on the programming language used in the framework.

  • If you use C# then the extension of view will be .cshtml
  • If you use VB then the extension of view will be .vbhtml

Since a view file can contain programming language (C# or VB) code and HTML. Hence, by combining both the file we got .cshtml & .vbhtml

27. How to add a View in MVC application?

There are two ways to add a View in the Asp.Net MVC application.

  • From an action method:
    1. Go to the action method in a controller class
    2. Right-click on the action method block and choose Add View.
    3. A new window with the details of view will appear.
Add view
  • 4. Enter the name of the View.
    5. Choose Template (Create, Delete, Details, Edit, Empty, List). If you are choosing a template then you must choose Model class.
    6. To make this view a partial, check the checkbox — Create as a partial view
    7. To include default JS files check the Reference script libraries checkbox.
    8. Select a layout
    9. And click the Add button.
    10. A new view will be added to the respective folder.
  • From folder hierarchy:
    1. Go to Views folder available in the root.
    2. Choose a folder respected to the controller name and right-click
    3. Click Add, and then View.
    4.
    Repeat the above-written steps from 4 –10.

28. What is View Hierarchy?

The view hierarchy is the set of all the folders inside Views Folder.
When we add a new controller, automatically a new folder with the same name of controller is created inside the Views folder.
Hence for each controller, there will be a separate folder inside Views folder.
Along with these folders, there is another folder called Shared.

View Hierarchy
View Hierarchy

When a new view is added for a particular action method then it goes in the respected parent controller name folder.
The shared folder is a special folder that contains all the common views for all the controllers. For example Layout, Partial View, etc.

29. How to return a View from Action method?

We can return a view with the help of View methods of Controller Class.
To return view the return type of action method must be ViewResult or ActionResult.

Return view form controller
Return a view from controller

30. How to call a View from the browser?

In Asp.Net MVC a browser can not call a view directly. Each request goes to a controller’s action method and it is the responsibility of action method what to return for the current request.

--

--