Infosys ASP.NET MVC Recent Most common Interview Questions And Answers 2018
What are Filters in MVC?Answer: In MVC, controllers define action methods and these action methods generally have a one-to-one relationship with UI controls such as clicking a button or a link, etc. For example, in one of our previous examples, the UserController class contained methods UserAdd, UserDelete, etc.
But many times we would like to perform some action before or after a particular operation. For achieving this functionality, ASP.NET MVC provides feature to add pre and post action behaviors on controller's action methods.
Types of Filters:
ASP.NET MVC framework supports the following action filters:
Action Filters: Action filters are used to implement logic that gets executed before and after a controller action executes. We will look at Action Filters in detail in this chapter.
Authorization Filters: Authorization filters are used to implement authentication and authorization for controller actions.
Infosys ASP.NET MVC Recent Interview Questions Answers |
Result Filters: Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
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.
Action filters are one of most commonly used filters to perform additional data processing, or manipulating the return values or cancelling the execution of action or modifying the view structure at run time.
Mention what is the difference between Temp data, View, and View Bag?
Answer: 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 type and checks for null values to avoid 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.
Doesn’t require typecasting for 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 type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages
Explain what is the difference between View and Partial View?
Answer:
View:
It contains the layout page.
Before any view is rendered, viewstart page is rendered.
View might have markup tags like body, html, head, title, meta etc.
View is not lightweight as compare to Partial View.
Partial View:
It does not contain the layout page.
Partial view does not verify for a viewstart.cshtml.We cannot put common code for a partial view within the viewStart.cshtml.page.
Partial view is designed specially to render within the view and just because of that it does not consist any mark up.
We can pass a regular view to the RenderPartial method.
What are Actions in MVC?
Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type — “ActionResult” and it will be invoked from method — “InvokeAction()” called by controller.
What is Attribute Routing in MVC?
ASP.NET Web API supports this type routing. This is introduced in MVC5. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level like –
[Route(“{action = TestCategoryList}”)] — Controller Level
[Route(“customers/{TestCategoryId:int:min(10)}”)] — Action Level
Explain Dependency Resolution?
Dependency Resolver again has been introduced in MVC3 and it is greatly simplified the use of dependency injection in your applications. This turn to be easier and useful for decoupling the application components and making them easier to test and more configurable.
Explain Bundle.Config in MVC4?
“BundleConfig.cs” in MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like — jquery.validate, Modernizr, and default CSS references.
How route table has been created in ASP.NET MVC?
Method — “RegisterRoutes()” is used for registering the routes which will be added in “Application_Start()” method of global.asax file, which is fired when the application is loaded or started.
What are the options can be configured in AJAX helpers?
Below are the options in AJAX helpers:
Url — This is the request URL.
Confirm — This is used to specify the message which is to be displayed in confirm box.
OnBegin — Javascript method name to be given here and this will be called before the AJAX request.
OnComplete — Javascript method name to be given here and this will be called at the end of AJAX request.
OnSuccess — Javascript method name to be given here and this will be called when AJAX request is successful.
OnFailure — Javascript method name to be given here and this will be called when AJAX request is failed.
UpdateTargetId — Target element which is populated from the action returning HTML.
What is _ViewStart.cshtml?
_ViewStart.cshtml defines code which is executed before any code in any of the views is executed.It is applied to all the views in a subdirectory.For example following is commonly included in _ViewStart.cshtml file:
@{
ViewBag.Title = "~/Views/Shared/_Layout.cshtml";
}
The above razor block will set a common layout for all the views.We can included our own code also in _Layout.cshtml which we want execute before the code in any of the views is executed.
What are the new features of MVC 5?
Some of the features included in MVC5 are
Scaffolding
ASP.NET Identity
One ASP.NET
Bootstrap
Attribute Routing
Filter Overrides
What is Bundling and Minification?
Bundling and Minification is used for improving the performance of the application.Bundling reduces the number of HTTP requests made to the server by combining several files into a single bundle.Minification reduces the size of the individual files by removing unnecessary characters.
How can we prevent Cross Site Request Forgery(CSRF)?
To prevent CSRF we apply the ValidateAntiForgeryToken attribute to an action method:
[ValidateAntiForgeryToken()]
What Are The Possible Razor View Extensions?
Below are the two types of extensions razor view can have :
.cshtml : In C# programming language this extension will be used.
.vbhtml - In VB programming language this extension will be used.
Post a Comment