Posts

Showing posts with the label MVC

Difference between array, arraylist, List, Hashtable, Dictionary and SortedList in c#

Basic difference is that arrays are of fixed size. Whereas an ArrayList implements the list data structure and can dynamically grow. While arrays would be more performance that a list, a list would be far more flexible since you don't need to know the required size initially. Array - represents an old-school memory array - kind of like a alias for a normal type[] array. Can enumerate. Can't grow automatically. I would assume very fast insertion, retrieve and speed. ArrayList - automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but still pretty fast. These are used a lot in .NET List - one of my favorites - can be used with generics, so you can have a strongly typed array, e.g. List . Other than that, acts very much like ArrayList. Hashtable - plain old hashtable. O(1) to O(n) worst case. Can enumerate the value and keys properties, and do key/val pairs. Dictionary - same as above only strongly typed via generics, such a...

What are Scaffold templates in ASP.Net MVC?

Scaffolding in ASP.NET MVC is used to generate the Controllers,Model and Views for create, read, update, and delete CRUD functionality in an application. The scaffolding will be knowing the naming conventions used for models and controllers and views. Explain the types of Scaffoldings. Below are the types of scaffoldings : Empty Create Delete Details Edit List

What are AJAX Helpers in ASP.Net MVC?

AJAX Helpers are used to create AJAX enabled elements like as Ajax enabled forms and links which performs the request asynchronously and these are extension methods of AJAXHelper class which exists in namespace - System.Web.ASP.Net MVC. Below are the options in AJAX helpers : Url : This is the request URL. Confirm : This is used to specify the message which is to be displayed in confirm box. OnBegin : Javascript method name to be given here and this will be called before the AJAX request. OnComplete : Javascript method name to be given here and this will be called at the end of AJAX request. OnSuccess - Javascript method name to be given here and this will be called when AJAX request is successful. OnFailure - Javascript method name to be given here and this will be called when AJAX request is failed. UpdateTargetId : Target element which is populated from the action returning HTML

Explain the methods used to render the views in ASP.Net MVC?

Below are the methods used to render the views from action - View() : To return the view from action. PartialView() : To return the partial view from action. RedirectToAction() : To Redirect to different action which can be in same controller or in different controller. Redirect() : Similar to "Response.Redirect()" in webforms, used to redirect to specified URL. RedirectToRoute() : Redirect to action from the specified URL but URL in the route table has been matched.

Name a few different return types of a controller action method?

The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class. 1. ViewResult 2. JavaScriptResult 3. RedirectResult 4. ContentResult 5. JsonResult

What are the advantages of ASP.NET MVC?

1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested. 2. Complex applications can be easily managed 3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller. 4. ASP.NET MVC views are light weight, as they donot use viewstate.

Can we have a Custom View Engine in MVC ?

Yes we have a Custom View Engine in MVC , by implementing the IViewEngine interface or by inheriting from the VirtualPathProviderViewEngine abstract class.

What is the significance of NonActionAttribute in MVC

In general, all public methods of a controller class are treated as action methods. If you want prevent this default behaviour, just decorate the public method with NonActionAttribute.

What is Web API ‘s in Asp.Net MVC 4 ?

◾Web API is a new framework for consuming & building HTTP Services. ◾Web API supports wide range of clients including different browsers and mobile devices. ◾It is very good platform for developing RESTful services since it talk’s about HTTP.

What is Attribute Routing in MVC?

ASP.NET Web API supports this type routing. This is introduced in 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 Just add the method – “MapMvcAttributeRoutes()” to enable attribute routing as shown below public static void RegistearRoutes(RouteCollection routes) { routes.IgnoareRoute("{resource}.axd/{*pathInfo}"); //enabling attribute routing routes.MapMvcAttributeRoutes(); //convention-based routing routes.MapRoute ( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Customer", action = "GetCustomerList", id = UrlParameter.Optional } ); }

What are the advantages of MVC over ASP.NET?

Following are advantages of MVC over ASP.NET Provides a clean separation of concerns among UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller). Easy to UNIT Test. Improved reusability of model and views. We can have multiple views which can point to the same model and vice versa. Improved structuring of the code.

What are the namespace classes used in ASP.NET MVC?

-All the namespaces and classes used for ASP.NET MVC reside in the System.Web.Mvc assembly. -System.Web.Mvc namespace This namespace provides classes and interfaces that support the MVC pattern for ASP.NET Web applications. This namespace also contains classes that manage controllers, controller factories, partial views, action results, views and model binders. -System.Web.Mvc.Ajax namespace This namespace provides classes that support Ajax scripts in an ASP.NET MVC application. The namespace also provides support of Ajax scripts and Ajax option settings. -System.Web.Mvc.Async namespace This namespace provides classes and interfaces that support asynchronous actions in an ASP.NET MVC application. -System.Web.Mvc.Html namespace This namespace provides classes that help in rendering HTML controls in an MVC application. The namespace contains classes providing forms, input controls, links, partial views, and validation support

What is Repository Pattern in ASP.NET MVC?

-Repository pattern is used as a default entity operation that allow the decoupling of the components used for presentation. -Repository pattern allows easy testing in the form of unit testing and mocking. -Repository pattern will have the proper infrastructure services to be used in the web applications. -It uses the mechanism of encapsulating that provides storage, retrieval and query for the implementation of the repository. -Repository patterns are hard coded in the application that is to be used in ASP.NET MVC architecture -Repository pattern is useful for decoupling entity operations form presentation, which allows easy mocking and unit testing. -The Repository will delegate to the appropriate infrastructure services to get the job done. Encapsulating in the mechanisms of storage, retrieval and query is the most basic feature of a Repository implementation. -Repository pattern is useful for decoupling entity operations form presentation, which allows easy mocking and unit testing...

What is a ViewModel in ASP.NET MVC?

A ViewModel basically acts as a single model object for multiple domain models, facilitating to have only one optimized object for View to render. Below diagram clearly express the idea of ViewModel in ASP.NET MVC: There are multiple scenarios where using ViewModel becomes obvious choice. For example: ◾Parent-Child View Scenario ◾Reports where often aggregated data required ◾Model object having lookup data ◾Dashboards displaying data from multiple sources

What is a ViewEngine in ASP.NET MVC?

“View Engine in ASP.NET MVC is used to translate our views to HTML and then render to browser.” There are few View Engines available for ASP.NET MVC but commonly used View Engines are Razor, Web Forms/ASPX, NHaml and Spark etc. Most of the developers are familiar with Web Forms View Engine (ASPX) and Razor View Engine. ◾Web Form View Engine was with ASP.NET MVC since beginning. ◾Razor View Engine was introduced later in MVC3. ◾NHaml is an open source view engine since 2007. ◾Spark is also an open source since 2008.

What is a ViewEngine in ASP.NET MVC?

“View Engine in ASP.NET MVC is used to translate our views to HTML and then render to browser.” There are few View Engines available for ASP.NET MVC but commonly used View Engines are Razor, Web Forms/ASPX, NHaml and Spark etc. Most of the developers are familiar with Web Forms View Engine (ASPX) and Razor View Engine. ◾Web Form View Engine was with ASP.NET MVC since beginning. ◾Razor View Engine was introduced later in MVC3. ◾NHaml is an open source view engine since 2007. ◾Spark is also an open source since 2008.

request flow in ASP.NET MVC framework

Request flow for ASP.NET MVC framework is as follows:  Request hits the controller coming from client. Controller plays its role and decides which model to use in order to serve the request. Further passing that model to view which then transforms the model and generate an appropriate response that is rendered to client.

Core features of ASP.NET MVC

Core features of ASP.NET MVC framework are: ◾Clear separation of application concerns (Presentation and Business Logic). It reduces complexity that makes it ideal for large scale applications where multiple teams are working. ◾It’s an extensible as well as pluggable framework. We can plug components and further customize them easily. ◾It provides extensive support for URL Routing that helps to make friendly URLs (means friendly for human as well as Search Engines). ◾It supports for Test Driven Development (TDD) approach. In ASP.NET WebForms, testing support is dependent on Web Server but ASP.NET MVC makes it independent of Web Server, database or any other classes. ◾Support for existing ASP.NET features like membership and roles, authentication and authorization, provider model and caching etc.

Difference between ASP.NET MVC and ASP.NET WebForms?

ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.

MVC Interview Question with Answer Part 2

What is difference between TempData and ViewData ? “TempData” maintains data for the complete request while “ViewData” maintains data only from Controller to the view. 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. 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[“MyData”]; TempData.Keep(“MyData”); The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well advices MVC to maintain “TempData” for the subsequent request. string str = TempData.Peek("Td...

ASP.NET MVC Interview Question with answer Part 1

Image
What is MVC (Model View Controller)? MVC is an architectural pattern which separates the representation and user interaction. It’s divided into three broader sections, Model, View, and Controller. Below is how each one of them handles the task. The View is responsible for the look and feel. Model represents the real world object and provides data to the View. The Controller is responsible for taking the end user request and loading the appropriate Model and View. Figure: MVC (Model view controller) Explain MVC application life cycle? There are six broader events which occur in MVC application life cycle below diagrams summarize it. Any web application has two main execution steps first understanding the request and depending on the type of the request sending out appropriate response. MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser. Creating the request object: - The request objec...