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