MVC Study Set

The flashcards below were created by user caldreaming on FreezingBlue Flashcards.

  1. What is the difference between 3 tier architecture and a mvc pattern?
    In MVC : MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model

    In Three Tier : A three tier architecture is the client tier never communicates directly with the data tier In a Three-tier model all communication must pass through the middle tier

    At first glance, the three tiers may seem similar to the model-view-controller (MVC) concept; however, topologically they are different. A fundamental rule in a three tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middle tier. Conceptually the three-tier architecture is linear. However, the [model-view-controller] MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model.
  2. What is N-Tier architecture?
    In software engineering, multitier architecture (often referred to as n-tier architecture) is a client–server architecture in which presentation, application processing, and data management functions are physically separated. The most widespread use of multitier architecture is the three-tier architecture.N-tier application architecture provides a model by which developers can create flexible and reusable applications. By segregating an application into tiers, developers acquire the option of modifying or adding a specific layer, instead of reworking the entire application. A three-tier architecture is typically composed of a presentation tier, a domain logic tier, and a data storage tier.

    Image Upload
  3. One of the core tenets of ASP.Net is known as 'DRY'.  What is DRY?
    One of the core design tenets of ASP.NET MVC is DRY ("Don't Repeat Yourself"). ASP.NET MVC encourages you to specify functionality or behavior only once, and then have it be reflected everywhere in an application. This reduces the amount of code you need to write and makes the code you do write less error prone and easier to maintain.The validation support provided by ASP.NET MVC and Entity Framework Code First is a great example of the DRY principle in action. You can declaratively specify validation rules in one place (in the model class) and the rules are enforced everywhere in the application.
  4. How do you do validation in MVC?
    The validation support provided by ASP.NET MVC and Entity Framework Code First is a great example of the DRY principle in action. You can declaratively specify validation rules in one place (in the model class) and the rules are enforced everywhere in the application.
  5. What are the benefits of using MVC?
    There are two big benefits of MVC:

    Separation of concerns is achieved as we are moving the code-behind to a separate class file. By moving the binding code to a separate class file we can reuse the code to a great extent.

    Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.
  6. Is MVC different from a three layered architecture?
    MVC is an evolution of a three layered traditional architecture. Many components of the three layered architecture are part of MVC.

    • Below is how the mapping goes:
    • Image Upload
  7. What is Razor?
    Razor is not a programming language. It's a server side markup language that lets you embed server-based code (Visual Basic and C#) into web pages.Server-based code can create dynamic web content on the fly, while a web page is written to the browser.

    When a web page is called, the server executes the server-based code inside the page before it returns the page to the browser. By running on the server, the code can perform complex tasks, like accessing databases. Razor is based on ASP.NET, and designed for creating web applications. It has the power of traditional ASP.NET markup, but it is easier to use, and easier to learn.
  8. What are the benefits of ASP.Net 4?
    • ASP.NET Web API
    • Refreshed and modernized default project templates
    • New mobile project template
    • Many new features to support mobile apps
    • Enhanced support for asynchronous methods
  9. What are the benefits of ASP.Net 5?
    • One ASP.NET
    • Attribute based routing
    • Asp.Net Identity
    • Bootstrap in the MVC template
    • Authentication Filters
    • Filter overrides
  10. What are the features of Razor?
    • Below is a short description of some useful Razor helpers:
    • Web Grid
    • Web Graphics
    • Google Analytics
    • Facebook Integration
    • Twitter Integration
    • Sending Email
    • Validation
  11. What are HTML helpers in MVC?
    HTML helpers help you to render HTML controls in the view. For instance if you want to display a HTML textbox on the view , below is the HTML helper code.

    <%= Html.TextBox("LastName") %>

    For a checkbox, below is the HTML helper code. In this way we have HTML helper methods for every HTML control that exists.

    <%= Html.CheckBox("Married") %>
  12. What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?
    Both of them provide the same HTML output, “HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t. Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.

    Html.TextBox("CustomerCode")

    Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘CustomerCode” from object “m”.

    TextBoxFor(m => m.CustomerCode)

    In the same way we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”.
  13. What is routing in MVC?
    Routing helps you to define a URL structure and map the URL with the controller. For instance let’s say we want that when a user types “http://localhost/View/ViewCustomer/”, it goes to the “Customer” Controller and invokes the DisplayCustomer action. This is defined by adding an entry in to the routes collection using the maproute function.

    Below is the underlined code which shows how the URL structure and mapping with controller and action is defined.Hide  

    • MapRoute(
    •      "View", // Route name
    •      "View/ViewCustomer/{id}", // URL with parameters
    •      new { controller = "Customer", action = "DisplayCustomer",
    • id = UrlParameter.Optional }); // Parameter defaults
  14. Where is the route mapping code written?
    The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.
  15. Can we map multiple URL’s to the same action?
    Yes, you can, you just need to make two entries with different key names and specify the same controller and action.
  16. Explain attribute based routing in MVC?
    This is a feature introduced in MVC 5. By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".

    • public class HomeController : Controller
    •         {
    •           [Route("Users/about")]
    •           public ActionResult GotoAbout()
    •            {
    •               return View();
    •            }
    •       }
  17. What is the advantage of defining route structures in the code?
    Most of the time developers code in the action methods. Developers can see the URL structure right upfront rather than going to the “routeconfig.cs” and see the lengthy codes. For instance in the below code the developer can see right upfront that the “GotoAbout” action can be invoked by four different URL structure. This is much more user friendly as compared to scrolling through the “routeconfig.cs” file and going through the lengthy lines of code to figure out which URL structure is mapped to which action.

    • public class HomeController : Controller
    •       {
    •          [Route("Users/about")]
    •          [Route("Users/WhoareWe")]
    •          [Route("Users/OurTeam")]
    •          [Route("Users/aboutCompany")]
    •       public ActionResult GotoAbout()
    •           {
    •             return View();
    •           }
    •    }
  18. How can we navigate from one view to another using a hyperlink?
    By using the ActionLink method as shown in the code below. This code will create a simple URL which helps to navigate to the “Home” controller and invoke the GotoHome action.

    <%= Html.ActionLink("Home","Gotohome") %>
  19. How can we restrict MVC actions to be invoked only by GET or POST?
    We can decorate the MVC action with the HttpGet or HttpPost attribute to restrict the type of HTTP calls. For instance you can see in the below code snippet the DisplayCustomer action can only be invoked by HttpGet. If we try to make HTTP POST on DisplayCustomer, it will throw an error.

    • [HttpGet]
    • public ViewResult DisplayCustomer(int id)
    •    {
    •       Customer objCustomer = Customers[id];
    •       return     View("DisplayCustomer",objCustomer);
    •     }
  20. How can we maintain sessions in MVC?
    Sessions can be maintained in MVC by three ways: tempdata, viewdata, and viewbag.
  21. What is the difference between tempdata, viewdata, and viewbag?
    • Temp data - Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.
    • View data - Helps to maintain data when you move from controller to view.
    • View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

    Image Upload
  22. We can maintain sessions in MVC with the use of Temp Data.  When would you use Temp Data?
    Temp data - Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

    Session variables - By using session variables we can maintain data from any entity to any entity.

    Image Upload
  23. We can maintain sessions in MVC with the use of View Data.  When would you use View Data?
    View data - Helps to maintain data when you move from controller to view.

    Image Upload
  24. We can maintain sessions in MVC with the use of View Bag.  When would you use View Bag?
    View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

    • Image Upload
    • Image Upload
  25. Describe the different mechanisms provided within MVC for persistence;
    Image Upload

    Image Upload
  26. What is difference between TempData and ViewData ?
    “TempData” maintains data for the complete request while “ViewData” maintains data only from Controller to the view.

    Image Upload
  27. Does “TempData” preserve data in the next request also?
    “TempData” is available through out for the current request and in the subsequent request it’s available depending on whether “TempData” is read or not.  So if “TempData” is once read it will not be available in the subsequent request.
  28. What is the use of Keep and Peek in “TempData”?
    Once “TempData” is read in the current request it’s not available in the subsequent request. If we want “TempData” to be read and also available in the subsequent request then after reading we need to call “Keep” method as shown in the code below.

    • @TempData[&ldquo;MyData&rdquo;];
    • TempData.Keep(&ldquo;MyData&rdquo;);

    The shortcut way of achieving the same is by using “Peek”. This function helps to read TempData and advices MVC to maintain “TempData” for the subsequent request.

    string str = TempData.Peek("Td").ToString();
  29. What are partial views in MVC?
    Partial view is a reusable view (like a user control) which can be embedded inside other view. For example let’s say all your pages of your site have a standard structure with left menu, header, and footer as shown in the image below;

    Image Upload

    For every page you would like to reuse the left menu, header, and footer controls. So you can go and create partial views for each of these items and then you call that partial view in the main view.
  30. How did you create a partial view and consume it?
    When you add a view to your project you need to check the “Create partial view” check box;

    Image Upload

    Once the partial view is created you can then call the partial view in the main view using the Html.RenderPartial method as shown in the below code snippet:

    • <body>
    •       <div>
    •          <% Html.RenderPartial("MyView"); %>
    •       </div>
    • </body>
  31. How can we do validations in MVC?
    • One of the easiest ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties. For example, in the below code snippet we have a simple Customer class with a property customercode.This CustomerCode property is tagged with a Required data annotation attribute. In other words if this model is not provided customer code, it will not accept it.
    • Image Upload
    • In order to display the validation error message we need to use the ValidateMessageFor method which belongs to the Html helper class.
    • Image Upload
    • Later in the controller we can check if the model is proper or not by using the ModelState.IsValid property and accordingly we can take actions.
    • Image Upload
  32. How can you tell whether changes have been made to the dataset since it was loaded?
    Microsoft provides methods for this purpose;

    DataSet.GetChanges() - Gets a copy of the DataSet that contains all changes made to it since it was loaded or since AcceptChanges() was last called.  

    DataSet.AcceptChanges() - Commits all changes made to this DataSet since it was loaded or since the last time AcceptChanges was called.
  33. What is CSRF attack and how can we prevent the same in MVC?
    • CSRF stands for Cross site request forgery. So if you see the dictonary meaning of forgery:
    • -“It’s an act of copying or imitating things like signature on a cheque, official documents to deceive the authority source for financial gains.”

    So when it comes to website this forgery is termed as CSRF (Cross Site Request Forgery).

    CSRF 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. Genuine site processes the information innocently thinking that data is coming from a trusted source.

    • So a proper solution to this issue can be solved by using tokens:
    • -End user browses to the screen of the money transfer. Before the screen is served server injects a secret token inside the HTML screen in form a hidden field.
    • Now hence forth when the end user sends request back he has to always send the secret token. This token is validated on the server.

    • Implementing token is a two-step process in MVC:
    • -First apply “ValidateAntiForgeryToken” attribute on the action.

    Second in the HTML UI screen call “@Html.AntiForgeryToken()” to generate the token.
  34. What does scaffolding use internally to connect to database?
    It uses Entity framework internally.
  35. How can we do exception handling in MVC?
    In the controller you can override the “OnException” event and set the “Result” to the view name which you want to invoke when error occurs. In the below code you can see we have set the “Result” to a view named as “Error”.

    • To display the above error in view we can use the below code;
    • Image Upload
  36. What does CRUD stand for?
    Create, Read, Update

    "Scaffolding" is a technique in which the MVC template helps to auto-generate CRUD code.
  37. Explain the concept of MVC scaffolding;
    Scaffolding is a technique in which the MVC template helps to auto-generate CRUD code. CRUD stands for create, read, update and delete.

    • So to generate code using scaffolding technique we need to select one of the types of templates (leave the empty one).
    • Image Upload
  38. In regards to CSS, what is Sass?
    Sass stands for Syntactically Awesome Style Sheets.

    Sass is completely compatible with a  versions of CSS.  Sass boasts more features and abilities than any other CSS extension language out there.  There are an endless number of frameworks built with Sass;  Compass, Bourbon, Susy just to name a few.
  39. What does Sass use the dollar symbol ($) for?
    • Sass uses the $ symbol to make something a variable.  Here's an example;
    • Image Upload
Author:
caldreaming
ID:
315389
Card Set:
MVC Study Set
Updated:
2016-02-11 21:56:09
Tags:
MVC model view controller asp net framework
Folders:

Description:
MVC Study Set
Show Answers: