Search This Blog

Tuesday, October 6, 2020

Q1-Q5

Q1. When a request came in MVC how the page Lifecyle goes?
Q2. What are filters in MVC?
Q3. How ViewData, View Bag and Tempdata are different?
Q4. Authentication filter is at controller level or action level?
Q5. Is it possible to share a view across multiple controllers?
=======================================================================
 Q1. When a request came in MVC how the page life cycle goes?

Answer:
Below are the step by steps of request life cycle or mvc request pipeline
  1. If its a first call then Application_Start() of Global.asax.cs will be called. Application_Start() will register the routes. 
  2. At this stage when Routes and RouteTables comes into picture for the first time. 
  3. Incoming request look for route from route table. if match not found return 404 Http Status Code
  4. if match is found then MVC handler comes into picture. MVC handler create a controller instance which is going for process the request further. 
  5. After this action is invoked and we get actionResult as output. ViewResult, JsonResult, ContentResult, FileResult, EmptyResult, etc. Among all the ActionResults,

=======================================================================
Q2. What are filters in MVC?

Answer:

sometimes we want to perform logic either before or after an action method is called. To support this, ASP.NET MVC provides filters. Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods.

There are 5 types of filters as of MVC 5

Authentication-IAuthenticationFilter-These are Runs, before any other filters or the action method.
Authorization-IAuthorizationFilter-These Runs first, before any other filters or the action method.
Action-IActionFilter-These Runs before and after the action method. (1) OnActionExecuting(), (2) OnActionExecuted()
Result-IResultFilter-Runs before and after the action result are executed.
Exception-IExceptionFilter-Runs only if another filter, the action method, or the action resultthrows an exception.


We have to simply implement the interface to override any functionality. 


=======================================================================
Q3. How ViewData, View Bag and Tempdata are different?

Answer:
pass data from the controller to view, either ViewData or ViewBag. 
To pass data from one controller to another controller, TempData can be used.

ViewData
  1. ViewData is used to pass data from controller to view
  2. It is derived from ViewDataDictionary class
  3. It is available for the current request only
  4. Requires typecasting for complex data type and checks for null values to avoid error
  5. If redirection occurs, then its value becomes null
ViewBag
  1. ViewBag is also used to pass data from the controller to the respective view
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only
  4. If redirection occurs, then its value becomes null
  5. Doesn’t require typecasting for complex data type
TempData
  1. TempData is derived from TempDataDictionary class
  2. TempData is used to pass data from the current request to the next request
  3. 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
  4. 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


=======================================================================
Q4. Authentication filter is at controller level or action level?

Answer:
This can be at both action level and controller level. 

=======================================================================
Q5. Is it possible to share a view across multiple controllers?

Answer:
Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

No comments:

Post a Comment

Q46-Q50

Q46.  If we have 3 types of constructor in code static, default, parametrized. In which order they will be called.  Q47. What is GAC? Q48.  ...