0% found this document useful (0 votes)
41 views

Mvc Interview

Asp .net MVC important questions.

Uploaded by

Faiz Syed
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Mvc Interview

Asp .net MVC important questions.

Uploaded by

Faiz Syed
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

s1. What is MVC (Model View Controller)?

Model–view–controller (MVC) is a software architectural pattern for implementing


user interfaces. It divides a software application into three parts.
Model, View and Controller.

The Model is the part of the application that handles the logic for the application
data.
model objects retrieve data (and store data) from a database.

The View is the part of the application that handles the display of the data.
views are created from the model data.

The Controller is the part of the application that handles user interaction.
Typically controllers read data from a view, control user input, and send input
data to the model.

2. List out different return types of a controller action method?


ViewResult (View)
This return type is used to return a webpage from an action method.

PartialviewResult (Partialview)
This return type is used to send a part of a view that will be rendered in another
view.

RedirectResult (Redirect)
This return type is used to redirect to any other controller and action method
depending on the URL.

RedirectToRouteResult (RedirectToAction, RedirectToRoute)


This return type is used when we want to redirect to any other action method.

ContentResult (Content)
This return type is used to return HTTP content type like text/plain as the result
of the action.

jsonResult (json)
This return type is used when we want to return a JSON message.

javascriptResult (javascript)
This return type is used to return JavaScript code that will run in the browser.

FileResult (File)
This return type is used to send binary output in response.

EmptyResult
This return type is used to return nothing (void) in the result.

3. What are the Filters in MVC?


ASP.NET MVC Filter is a custom class where we can write custom logic to execute
before or after an action method executes.
Types of Filters

ASP.NET MVC framework supports the following action filters,

Action Filters
Performs some operation before and after an action method executes.
Authorization Filters
Authorization filters are used to implement authentication and authorization for
controller actions.

Result Filters
Performs some operation before or after the execution of the view.

Exception Filters
Exception filters are the last type of filter to run. You can use an exception
filter to handle errors raised by either your controller actions or controller
action results. You can also use exception filters to log errors.

4.Routing in MVC.
In MVC, routing is a process of mapping the browser request to the controller
action and return response back.
Each MVC application has default routing for the default HomeController. We can set
custom routing for newly created controller.

The RouteConfig.cs file is used to set routing for the application.

There are two types of routing.

Convention-based routing - to define this type of routing, we call MapRoute method


and set its unique name, URL pattern and specify some default values.
Attribute-based routing - to define this type of routing, we specify the Route
attribute in the action method of the controller.

5. How to pass data between controllers and view.

In ASP.NET MVC there are three ways to pass/store data between the controllers and
views.

ViewData
ViewData is used to pass data from controller to view.
It is derived from ViewDataDictionary class.
It is available for the current request only.
Requires typecasting for complex data types and checks for null values to avoid an
error.
If redirection occurs, then its value becomes null.

ViewBag
ViewBag is also used to pass data from the controller to the respective view.
ViewBag is a dynamic property that takes advantage of the new dynamic features in
C# 4.0
It is also available for the current request only.
If redirection occurs, then its value becomes null.
It doesn’t require typecasting for the complex data type.

TempData
TempData is derived from TempDataDictionary class
TempData is used to pass data from the current request to the next request
It keeps the information for the time of an HTTP Request. This means only from one
page to another. It helps to maintain the data when we move from one controller to
another controller or from one action to another action
It requires typecasting for complex data types and checks for null values to avoid
an error. Generally, it is used to store only one time messages like the error
messages and validation messages
View:
A "View" in ASP.NET MVC is a template or a web page that is responsible for
presenting the user interface to the end user.
It typically contains HTML markup mixed with server-side code that renders dynamic
content by accessing data from the model.
Views are essential for displaying the final user interface to the client, and they
often correspond to a specific URL or route in your application.
Views are typically complete web pages that include the full structure of a page,
including HTML, CSS, JavaScript, and any dynamic content.

Partial View:
A "Partial View" is a reusable and modular component that represents a portion of a
web page, rather than an entire page.
Partial Views are used when you want to break down your page into smaller, self-
contained components that can be used in multiple places across your application.
They are typically designed to be embedded within regular views and can be invoked
using the @Html.Partial() or @Html.RenderPartial() helper methods in your views.
Partial Views allow for better code organization, reusability, and the separation
of concerns in your application, making it easier to maintain and update specific
parts of your pages independently.

You might also like