Sunday 12 March 2017

MVC interview questions Part 6

1. Ques: How can we use two (multiple) models with a single view ?
Answer:
10 ways to bind multiple models on a single view given bellow

1. View Model
ViewModel is nothing but a single class that may have multiple models. It contains multiple models as a property. It should not contain any method.

2. View Bag
ViewBag is similar to ViewData and is also used to transfer data from the controller to the view. ViewBag is a dynamic property. ViewBag is just a wrapper around the ViewData.

3. View Data
ViewData is used to transfer data from the controller to the view. ViewData is a dictionary object that may be accessible using a string as the key. Using ViewData, we can pass any object from the controller to the view. The Type Conversion code is required when enumerating in the view.

4. Temp Data
TempData is also a dictionary derivative from TempDataDictionary class. TempData stored in short lives session. We can pass multiple models through TempData also.

5. Session
Session is used to pass data across controllers in MVC Application.Session data never expires (by default session expiration time is 20 minutes but it can be increased).Session is valid for all requests, not for a single redirect.

6. Dynamic Model
ExpandoObject (the System.Dynamic namespace) is a class that was added to the .Net Framework 4.0 that allows us to dynamically add and remove properties onto an object at runtime.

7. Tuples
A Tuple object is an immutable, fixed-size and ordered sequence object. It is a data structure that has a specific number and sequence of elements. The .NET framework supports tuples up to seven elements.

8. Render Action
A Partial View defines or renders a partial view within a view. We can render some part of a view by calling a controller action method using the Html.RenderAction method. The RenderAction method is very useful when we want to display data in the partial view. The disadvantages of this method is that there are only multiple calls of the controller.

9. JSON
We can Bind Multiple Models with the help of Json as well. We will retun JsonResult from action Method and on View through JQuery we can pasrse the JSON data and Bind on View.

10. Navigation Properties
If we have two related models then we can bind a model into another model as a property and can pass to a View.


2. Ques: What is csrf(Cross-Site Request Forgery) attack in mvc ? How Preventing (CSRF) Attacks ?
Answer:
CSRF (Cross site request forgery) is a method of attacking a website where the attacker imitates a.k.a forges as a trusted source and sends data to the site. CSRF stands for Cross site request forgery.

To Preventing CSRF attack  we need to add “@Html.AntiForgeryToken” in view with that we need to add this in controller also as a attribute [ValidateAntiForgeryToken] to validating this. Basically AntiForgeryToken is used in HTTPPost method.


3. Ques: What is diffrence between @Html.Action and @Html.RenderAction in mvc ?
Answer :

@Html.Action

This Html.Action renders partial view as an HTML string so we can store it in another string variable. It is string return type method so first it returns result as a string then renders result to response.

@Html.RenderAction

This is also same as Html.Action but main difference is that it renders result directly to response that’s why it is more efficient if the action returns a large amount of HTML over @Html.Action.


4. Ques: How can you remove default View Engine in ASP.NET MVC? ?
Answer:
We can customize view engines in ASP.NET MVC application. If you are not using any view engine like ASPX View Engine, better you remove it to improve the performance.

Removing the Web Form view engine is easy in MVC. We can remove all the view engines and add only Razor view engine by using Application_Start event of Global.asax.cs file like below.

protected void Application_Start()
{
//Remove All Engine
 ViewEngines.Engines.Clear();
 //Add Razor Engine
 ViewEngines.Engines.Add(new RazorViewEngine());
}

5. Ques:  What are Non Action methods in ASP.Net MVC?
Answer:
In ASP.Net MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with "NonAction" attribute as shown below :

[NonAction]
public void TestMethod()
{
// Method logic
}


6. Ques:  What is the "HelperPage.IsAjax" Property?
Answer:
The HelperPage.IsAjax property gets a value that indicates whether Ajax is being used during the request of the Web page.


7. Ques:  What is Attribute Routing in ASP.Net MVC?
Answer:
ASP.NET Web API supports this type routing. This is introduced in ASP.Net 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


8. Ques:  What is ViewStart? What are the benifit for ViewStart ? 
Answer:
Razor View Engine introduced a new layout named _ViewStart which is applied on all view automatically. Razor View Engine firstly executes the _ViewStart and then start rendering the other view and merges them.

  • _ViewStart.cshtml page is a special view page containing the statement declaration to include the Layout page.
  • Instead of declaring the Layout page in every view page, we can use the _ViewStart page.
  • When a View Page Start is running, the “_ViewStart.cshtml” page will assign the Layout page for it.

ex:
_viewstart.cshtml
@{
        Layout = "~/Views/Shared/_Layout.cshtml";
 }

Benifit for ViewStart
Instead of declaring the Layout page individually in every page, we can include it on one place only.
So the application became maintainable.
You can still add another page as your Layout page to individual pages, regardless of your _ViewStart page.

9 .What is JSON?
Answer:
JSON full form is JavaScript Object Notation. JSON is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. And JSON is language-independent, with parsers available for virtually every programming language. Uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python,php  The JSON format is often used for serializing and transmitting structured data over a network connection. When third party data interchane(REST Services) then JSON may used there LIKE SHOP .It is primarily used to transmit data between a server and web application, serving as an alternative to XML.


10. What is JsonResultType in MVC?
Answer:
JsonResult is an ActionResult type in MVC. It helps to send the content in JavaScript Obect Notation (JSON) format.
Action methods on controllers return JsonResult that can be used in an AJAX application. This class is inherited from the "ActionResult" abstract class. Here Json is provided one argument which must be serializable. The JSON result object that serializes the specified object to JSON format.

public JsonResult JsonResultTest()
{
    return Json("Hello My Friend!");
}   

No comments:

Post a Comment

Thank you for comment