Asp Net Interview Questions Geeksforgeeks

C# is a simple, modern and a general-purpose Object-oriented programming language developed by Microsoft within its .NET framework. In this C# Interview Questions blog, I will introduce you to the most commonly asked Interview Questions in C# and what should be your approach to do the same.

3 How can we pass the data From Controller To View In MVC?

Answer

There are three options in Model View Controller (MVC) for passing data from controller to view. This article attempts to explain the differences among ViewData, ViewBag and TempData with examples. ViewData and ViewBag are similar and TempData performs additional responsibility. The following are the key points on those three objects.

ViewData

  • The ViewData is used to move data from controller to view.
  • The ViewData is a dictionary of objects that are derived from the “ViewDataDictionary” class and it will be accessible using strings as keys.
  • ViewData contains a null value when redirection occurs.
  • ViewData requires typecasting for complex data types.
  • ViewBag

  • ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  • ViewBag doesnt require typecasting for complex data types.
  • ViewBag also contain a null value when redirection occurs.
  • TempData

  • ViewData moves data from controller to view.
  • Use TempData when you need data to be available for the next request, only. In the next request, it will be there but will be gone after that.
  • TempData is used to pass data from the current request to the subsequent request, in other words in case of redirection. That means the value of TempData will not be null.
  • Learn more here – Various Ways to Pass Data From Controller to View in MVC 4

    Q Define method overloading in C#?

    Ans- Method overloading essentially is creating multiple methods with the same name and unique signatures with the same class. When you compile, the compiler makes use of overload resolution to determine the specific method which will be invoked.

    3 How can we done Custom Error Page in MVC?

    Answer

    The HandleErrorAttribute allows you to use a custom page for this error. First you need to update your web.config file to allow your application to handle custom errors.

    Then, your action method needs to be marked with the atttribute.

    By calling the ThrowException action, this would then redirect the user to the default error page. In our case though, we want to use a custom error page and redirect the user there instead.So, lets create our new custom view page.

    Next, we simply need to update the HandleErrorAttribute on the action method.

    Learn more here – Custom Error Page in ASP.NET MVC

    Q2 What is an Interface?

    Ans- An Interface is basically a class with no implementation. It contains only the declaration of properties, attributes, and events.

    1 What is model binding in ASP.NET?

    Controllers and views need to work with data that comes from HTTP requests. For example, routes may provide a key that identifies a record, and posted form fields may provide model properties. The process of converting these string values to .NET objects could be complicated and something that you have to do with each request. Model binding automates and simplifies this process.

    The model binding system fetches the data from multiple sources such as form fields, route data, and query strings. It also provides the data to controllers and views in method parameters and properties, converting plain string data to .NET objects and types in the process.

    Example: Let’s say you have the following action method on the PostsController class:

    And the app receives a request with this URL:

    After the routing selects the action method, model binding executes the following steps.

  • Locate the first parameter of GetByID, an integer named id, look through the available sources in the HTTP request and find id = “5” in route data.
  • Convert the string “5” into an integer 5.
  • Find the next parameter of GetByID, a boolean named archivedOnly.
  • Look through the sources and find “ArchivedOnly=true” in the query string. It ignores the case when matching the parameters to the strings.
  • Convert the string “true” into boolean true.
  • Some other examples of attributes include: 1. [FromQuery] – Gets values from the query string. 2. [FromRoute] – Gets values from route data. 3. [FromForm] – Gets values from posted form fields. 4. [FromBody] – Gets values from the request body. 5. [FromHeader] – Gets values from HTTP headers.

    .NET Interview Questions And Answers | ASP.NET Interview Questions And Answers | 2022 | Simplilearn

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *