Sunday, May 24, 2015

HTTPHandler and HTTPModule in ASP.NET

9 comments Posted by Rahul Kharde at 10:10 AM

If you want to implement pre-processing logic before a request hits the IIS resources. For instance you would like to apply security mechanism, URL rewriting, filter something in the request, etc. ASP.NET has provided two types of interception HttpModule and HttpHandler.  

The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension. 

For Example

When an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse.

HTTPModule:
-   It's just like a filter. The Modules are called before and after the handler executes.
-   HTTP Modules are objects which also participate the pipeline but they work before and after the HTTP Handler does its job, and produce additional services within the pipeline
-   Modules implement the IHttpMudule interface located in the System.Web.System.

For example associating session within a request before HTTP handler executes, and saving the session state after HTTP handler has done its job, is basically done by an HTTP module, SessionStateModule

HTTPHandler:
-   HttpHandler is the one responsible for handling the request i.e. to generate HTTP response which often (but not always) means generating some html.
-   HTTP handlers are the end point objects in ASP.NET pipeline and an HTTP Handler essentially processes the request and produces the response.
-   It executes some code when the user sends some request. An .aspx page can be thought as a HTTPHandler too, which implements more functions. 

For example an ASP.NET Page is an HTTP Handler.



Read More »

MVC5 Interview Questions?

1 comments Posted by Rahul Kharde at 10:09 AM


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.

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.

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:
  •  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.


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" />


Read More »

Friday, May 22, 2015

Bootstrap for MVC5

0 comments Posted by Rahul Kharde at 12:11 PM


 Bootstrap (a front-end framework) is an open source collection of tools that contains HTML and CSS-based design templates along with Javascript to create a responsive design for web applications.

  • The Bootstrap front-end framework 
  • Bootstrap is a sleek, intuitive, and powerful front-end framework for faster and easier web development.
  • BootStrap , created and maintained by Mark Otto and Jacob Thornton
  • Bootstrap is available for MVC4 via Nuget or by default in new MVC5 projects.
  • Bootstrap provides a base collection including layouts, base CSS,JavaScript widgets, customizable components and plugins.
  • Over a dozen reusable components built to provide iconography, dropdowns, navigation, alerts, popovers, and much more.
  • Server- and client-side validation based on DataAnnotations attributes
  • Globalization and localization
  • Rich text using Markdown
  • Bootstrap datepicker, timepicker and file-upload controls
  • Use of MVC EditorTemplates



Read More »

Thursday, May 21, 2015

What is the difference between ViewData, ViewBag and TempData?

0 comments Posted by Rahul Kharde at 11:09 AM
ViewData
ViewBag
TempData
ViewData is used to pass data from controller to view
ViewBag is also used to pass data from the controller to the respective view
TempData is derived from TempDataDictionary class
ViewData is a dictionary object and it is property of ControllerBase class
ViewBag is Dynamic property of ControllerBase class.
TempData is a dictionary object and it is property of controllerBase class.
ViewData is Faster than ViewBag
ViewBag is slower than ViewData
--
If redirection occurs, then its value becomes null
If redirection occurs, then its value becomes null
TempData helps to maintain the data when we move from one controller to another controller or from one action to another action
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above
ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above
TempData is also introduced in MVC1.0 and available in MVC 1.0 and above.
ViewData  is also work with .net framework 3.5 and above
ViewBag  is only  work with .net framework 4.0 and above
TempData  is also work with .net framework 3.5 and above
Type Conversion code is required while enumerating
In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating.
Type Conversion code is required while enumerating
It lies only during the current request.
It lies only during the current request.
TempData is only work during the current and subsequent request
Read More »
 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top