Search This Blog

Tuesday, October 27, 2020

Q6-Q10

Q6. Where are the routing rules defined in an asp.net MVC application?
Q7. Name a few different return types of a controller action method in MVC?
Q8. What is the difference between ViewResult() and ActionResult() in ASP.NET MVC?
Q9. Difference between @Html.TextBox and @Html.TextBoxFor
Q10. What is the testing framework you have used?
======================================================================
Q6. Where are the routing rules defined in an asp.net MVC application?

Answer:
Asp.net MVC - In Application_Start event in Global.asax

.netCore MVC - in Startup.cs, configure() methods. app.UseEndpoints middleware. 

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
======================================================================
Q7. Name a few different return types of a controller action method in MVC?

Answer:
There 12 kinds of results in MVC, 
at the top is “ActionResult” class which is a base class that can have 11 subtypes’ as listed below: -
  1. ViewResult -        Renders a specified view to the response stream
  2. PartialViewResult - Renders a specified partial view to the response stream
  3. EmptyResult -       An empty response is returned
  4. RedirectResult -    Performs an HTTP redirection to a specified URL
  5. RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
  6. JsonResult -       Serializes a given ViewData object to JSON format
  7. JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
  8. ContentResult -    Writes content to the response stream without requiring a view
  9. FileContentResult -Returns a file to the client
  10. FileStreamResult - Returns a file to the client, which is provided by a Stream
  11. FilePathResult -   Returns a file to the client
Knowledge bomb : 
IHttpActionResult is a way for creating responses introduced in WebAPI2 but IActionResult is more leaned towards ASP.NET MVC for returning the result of an action method. Also IActionResult is widely used in .net Core as well.
======================================================================
Q8. What is the difference between ViewResult() and ActionResult() in ASP.NET MVC?

Answer:
  • ActionResult is an abstract class.
  • ViewResult derives from ActionResult. Other derived classes include JsonResult and PartialViewResult.
  • The View() method returns a ViewResult. 
  • In case we want to return two types of data based on condition. In that case actionResult would be the choice. 










======================================================================
Q9. Difference between @Html.TextBox and @Html.TextBoxFor

Answer:
Html.TextBox is not strongly typed and it doesn't require a strongly typed view meaning that you can hardcode whatever name you want as first argument and provide it a value:
@Html.TextBox("foo", "some value")

You can set some value in the ViewData dictionary inside the controller action and the helper will use this value when rendering the textbox (ViewData["foo"] = "bar").

Html.TextBoxFor is requires a strongly typed view and uses the view model:
@Html.TextBoxFor(x => x.Foo)

The helper will use the lambda expression to infer the name and the value of the view model passed to the view.

And because it is a good practice to use strongly typed views and view models you should always use the Html.TextBoxFor helper.

======================================================================
Q10. What is the testing framework you have used?

Answer:
Visual Studio default Testing template can be used for Unit testing. It can be used with Mock testing framework.  It work on concept AAA
The AAA (Arrange, Act, Assert) pattern is a common way of writing unit tests for a method under test.
  • The Arrange section of a unit test method initializes objects and sets the value of the data that is passed to the method under test.
  • The Act section invokes the method under test with the arranged parameters.
  • The Assert section verifies that the action of the method under test behaves as expected.



My Other Blogs

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.

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.  ...