What are the methods of handling an Error in MVC?
Answer
Exception handling may be required in any application, whether it is a web application or a Windows Forms application.
ASP.Net MVC has an attribute called “HandleError” that provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as Controller or at the global level. The HandleError attribute is the default implementation of IExceptionFilter. When we create a MVC application, the HandleError attribute is added within the Global.asax.cs file and registered in the Application_Start event.
Important properties of HandleError attribute
The HandleError Error attribute has a couple for properties that are very useful in handling the exception.
ExceptionType
Type of exception to be catch. If this property is not specified then the HandleError filter handles all exceptions.
View
Name of the view page for displaying the exception information.
Master
Master View for displaying the exception.
Order
Order in which the action filters are executed. The Order property has an integer value and it specifies the priority from 1 to any positive integer value. 1 means highest priority and the greater the value of the integer is, the lower is the priority of the filter.
AllowMultiple
It indicates whether more than one instance of the error filter attribute can be specified.
Example
HandleError Attribute at Action Method Level,
Here are more details on Exception or Error Handling in ASP.Net MVC Using HandleError Attribute.
3 What is Scaffolding in MVC?
Answer
Scaffolding is a code generation framework for ASP.NET Web applications. Visual Studio 2013 includes pre-installed code generators for MVC and Web API projects. You add scaffolding to your project when you want to quickly add code that interacts with data models. Using scaffolding can reduce the amount of time to develop standard data operations in your project.
Prerequisites
To use ASP.NET Scaffolding, you must have,
What are the Advantages of using Scaffolding ?
Learn more here – Scaffolding In MVC 5
Answer
Razor View Engine introduced a new layout named _ViewStart which is applied on all view automatically. Razor View Engine firstly executes the _ViewStart and then start rendering the other view and merges them.
Example of Viewstart
Learn more here – Viewstart Page in ASP.NET MVC 3
2 How does View Data differ from View Bag in MVC?
View Data |
View Bag |
ViewData is used to pass data from a controller to view |
ViewBag is also used to pass data from the controller to the respective view. |
It is available for the current request only. |
It is also available for the current request only. |
Requires typecasting for complex data type and checks for null values to avoid error |
Doesn’t require typecasting for the complex data type. |
If redirection occurs, then its value becomes null. |
If redirection occurs, then its value becomes null. |
Mention what does Model-View-Controller represent in an MVC application?
Components | Description |
Model |
It represents the application data domain. In other words, applications business logic is contained within the model and is responsible for maintaining data. |
View |
It represents the user interface, with which the end-users communicates. In short, all the user interface logic is contained within the VIEW. |
Controller |
It is the controller that answers to user actions. Based on the user actions, the respective controller responds within the model and choose a view to render that display the user interface. The user input logic is contained with-in the controller. |
2. Explain what is MVC?
MVC is an abbreviation for Model, View, and Controller. The MVC architectural pattern separates an application into three components – Model, View, and Controller. In this pattern, the model represents the shape of the data and business logic. It maintains and preserves the data of the application. Model objects retrieve and store model state in a database. The view is basically and technically a user interface. The view segment displays the data-using model to the user and also enables them to modify the data. The controller is the part, which handles the user request.