MVC means ?
MVC stands for Model View Controller.
It divides an application into 3 component roles which is based on a framework methodology.
These component roles are discussed briefly as follows:
Models : These component roles are used to maintain the state which is persisted inside the Database.
Example: we might have a Product class that is used to represent order data from the Products table inside SQL.
Views : These component roles are used to display the user interface of the application, where this UI is created off of the model data.
Example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object.
Controllers : These component roles are used for various purposes like handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI.
In a MVC application, the views are used only for displaying the information whereas the controllers are used for handling and responding to user input and interaction.
It divides an application into 3 component roles which is based on a framework methodology.
These component roles are discussed briefly as follows:
Models : These component roles are used to maintain the state which is persisted inside the Database.
Example: we might have a Product class that is used to represent order data from the Products table inside SQL.
Views : These component roles are used to display the user interface of the application, where this UI is created off of the model data.
Example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object.
Controllers : These component roles are used for various purposes like handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI.
In a MVC application, the views are used only for displaying the information whereas the controllers are used for handling and responding to user input and interaction.
What
are the advantages of using asp.net mvc ?
The main advantages of using asp.net
mvc are as follows:
i) One of the main advantage is that it will be easier to manage the complexity as the application is divided into model,view and controller.
ii) It gives us the full control over the behavior of an application as it does not use view state or server-based forms.
iii) It provides better support for test-driven development (TDD).
iv) You can design the application with a rich routing infrastructure as it uses a Front Controller pattern that processes Web application requests through a single controller.
i) One of the main advantage is that it will be easier to manage the complexity as the application is divided into model,view and controller.
ii) It gives us the full control over the behavior of an application as it does not use view state or server-based forms.
iii) It provides better support for test-driven development (TDD).
iv) You can design the application with a rich routing infrastructure as it uses a Front Controller pattern that processes Web application requests through a single controller.
What is the use of a controller in an
MVC application ?
A controller will decide what to do and what to display in the view. It works as follows:
i) A request will be received by the controller
ii) Basing on the request parameters, it will decide the requested activities
iii) Basing on the request parameters, it will delegates the tasks to be performed
iv) Then it will delegate the next view to be shown
Mention some of the return types of a
controller action method ?
An action method is used to return an
instance of any class which is derived from ActionResult class.
Some of the return types of a controller action method are:
Some of the return types of a controller action method are:
- ViewResult : It is used to return a webpage from an action method
- PartialViewResult : It is used to send a section of a view to be rendered inside another view.
- JavaScriptResult : It is used to return JavaScript code which will be executed in the user’s browser.
- RedirectResult : Based on a URL, It is used to redirect to another controller and action method.
- ContentResult : It is an HTTP content type may be of text/plain. It is used to return a custom content type as a result of the action method.
- JsonResult : It is used to return a message which is formatted as JSON.
- FileResult : It is used to send binary output as the response.
- EmptyResult : It returns nothing as the result.
Explain
about 'page lifecycle' of ASP.NET MVC ?
The page lifecycle of an ASP.NET MVC page
is explained as follows:
1) App Initialisation
In this stage, the aplication starts up by running Global.asax’s Application_Start() method.
In this method, you can add Route objects to the static RouteTable.Routes collection.
If you’re implementing a custom IControllerFactory, you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property.
2) Routing
Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern.
MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table.
3) Instantiate and Execute Controller
At this stage, the active IControllerFactory supplies an IController instance.
4) Locate and invoke controller action
At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView().
5) Instantiate and render view
At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.
1) App Initialisation
In this stage, the aplication starts up by running Global.asax’s Application_Start() method.
In this method, you can add Route objects to the static RouteTable.Routes collection.
If you’re implementing a custom IControllerFactory, you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property.
2) Routing
Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern.
MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table.
3) Instantiate and Execute Controller
At this stage, the active IControllerFactory supplies an IController instance.
4) Locate and invoke controller action
At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView().
5) Instantiate and render view
At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.
What
is App_Start folder in ASP.NET MVC?
App_Start folder has been introduced in
MVC4. It contains various configurations files like as BundleConfig.cs,
FilterConfig.cs, RouteConfig.cs, WebApiConfig.cs for your application. All
these settings are registered within Application_Start method of Global.asax.cs
file.
- AuthConfig.cs: Used to configuration security settings, including sites for OAuth login
- BundleConfig.cs – This is used to create and register bundles for CSS and JS files. By default, various bundles are added in this files including jQuery, jQueryUI, jQuery validation, Modernizr, and Site CSS.
- FIlterConfig.cs – This is used to register global MVC filters like error filters, actions filters etc. By default it contains HandleErrorAttribute filter.
- RouteConfig.cs – This is used to register various route patterns for your ASP.NET MVC application. By default, one route is registered here named as Default Route.
- WebApiConfig.cs – This is used to register various WEB API routes like as ASP.NET MVC, as well as set any additional WEB API configuration settings.
Differnce between HTML.TextBox and HTML.TextBoxFor?
Html.TextBox
is not strongly
typed and it doesn't require a strongly typed view meaning that you can
hardcode whatever name you want as first argument and provide it a value:Html.TextBoxFor
is requires a
strongly typed view and uses the view model
Example:-
@Html.TextBox("Name")
@Html.TextBoxFor(m => m.Name)
both will produce
<input id="Name" name="Name" type="textbox" />
@Html.TextBox("Name")
@Html.TextBoxFor(m => m.Name)
both will produce
<input id="Name" name="Name" type="textbox" />
Nice post . Thanks for the shearing valuable information. I really fiend this type blog. Special thanks to writer.
ReplyDeleteAsp.net developers