Posts Tagged ‘HttpHandler’

ASP.Net 4.0 New Features

ViewStateMode – ViewState for Individual Controls
ASP.Net 4.0 allows view state in a page to be more controllable from page to its child controls level. That is, view state of a control can be enabled or disabled irrespective of its parent control’s view state. Even if view state of a page is disabled, controls of the page can have their own view state individually enabled or disabled or even inherited from the page’s view state mode property.
This property if utilized properly can certainly boost performance of a page.

For example, we can individually enable or disable user control’s view state in a page.


By default, ViewStateMode is enabled for a page object, while controls have inherit mode.

Page.MetaKeywords and Page.MetaDescription – SEO Optimization Feature
ASP.Net 4.0 has come up with these two properties that will help developers add meta tags for keywords and description in the aspx pages in easier fashion. Web Search Engines really need these two meta tags for search indexing of any pages. These two properties can be used in a page in various ways. Inside <head> tag or in the code behind or even at <%@Page%> directive level.

However, setting meta keywords or description in code behind will be more useful when we have to add keywords and descriptions dynamically from source like database.

MetaKeyWords is used to store few useful keywords that will briefly highlight important information of a page by tags. From SEO perspective, meta keywords should contain keywords separated by spaces.

MetaDescription is used to add page description in short that will help Search Engines to quickly describe about the page links in search pages.

Prior to ASP.Net 4.0, we have to add meta tags using HtmlMeta control (public class HtmlMeta : HtmlControl) adding into page header as:

protected void Page_Load(object sender, EventArgs e)
HtmlMeta metakey = new HtmlMeta();
metakey.Name = "keywords";
metakey.Content = "ASP.Net 2.0 3.5";
HtmlMeta metadesc = new HtmlMeta();
metadesc.Name = "description";
metadesc.Content = "ASP.Net 2.0 3.5 Page Description...";
//Add to page header

In ASP.Net 4.0, we can add in many ways.

protected void Page_Load(object sender, EventArgs e)
//Adding Page meta tags information
this.Page.MetaKeywords = "ASP.Net 4.0 SEO Meta Tag";
this.Page.MetaDescription = "Serializing and Deserializing Thoughts..";


<head runat="server">
<title>Feature: ViewStateMode</title>
<meta name="keywords" content ="ASP.Net 4.0 ViewStateMode"/>
<meta name="description" content="ViewStateMode feature in ASP.Net 4.0" />

Or inside Page directive,


Response.RedirectPermanent – Search Engine Friendly Webpage Redirection

In classic ASP or ASP.Net earlier than 4.0, we used to redirect to new pages or links by setting Response.StatusCode to 301 before calling Response.AddHeader method. Now ASP.Net 4.0 has provided Response.RedirectPermanent method to redirect to new pages or links with StatusCode of 301 implicitly set. Search Engines use this 301 code to understand permanent redirection from old pages links.

For example,

Classic ASP method:

<%@ Language=VBScript %>
Response.Status="301 Moved Permanently"
Response.AddHeader "Location",""

ASP.Net method prior to 4.0:

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
Response.Status = "301 Moved Permanently";

ASP.Net 4.0 method:

Response.RedirectPermanent(" ");

Web.Config Refactoring – Custom HttpHandlers and HttpModules

Web.config now looks cleaner as most of the settings are controlled from machine.config file as ASP.Net 4.0 is all set to benefit from IIS 7 and IIS 7.5 features. When IIS is set to use .Net 4.0 and Integrated Pipeline mode, <compilation> element holds .Net version attribute. And the traditional <httpHandlers> and <httpModules> section is now shifted out of <system.web> and added inside new section <system.webserver>. All the custom handlers are added inside <handlers>, and all the modules inside <modules> section.

<!-- Add the module for Integrated mode applications -->
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="WebAppModule.MyCustomModule, WebAppModule" />
<!-- Add the handler for Integrated mode applications -->
<add name="MyHandler" path="" verb="GET" 
<type="WebAppModule.MyCustomHandler, WebAppModule"
preCondition="integratedMode" /> </handlers> </system.webServer>


<compilation debug="true" targetFramework="4.0" />

Interesting point is, when we add custom handlers and modules this way, we do not have to manually configure handlers and modules in IIS again. IIS will automatically refresh itself.


Load ASP.Net User Control Dynamically Using jQuery

October 10, 2009 5 comments

Today we will explore the way of loading ASP.Net user control at run time using jQuery. jQuery has one method load(fn) that will help here. This load(fn) method has following definition.

load (url, data, callback): A GET request will be performed by default – but if any extra parameters are passed, then a POST will occur.
url (string): URL of the required page
data (map – key/value pair): key value pair data that will be sent to the server
callback (callback method): call back method, not necessarily success

Now comes custom HttpHandler that will load the required user control from the URL given by this load(fn) method. We all know that it is either in-built or custom HttpHandler that is the end point for any request made in ASP.Net.

Let’s see by example. In the ASP.Net application, add one aspx page and user control. Then, add one more class derived from IHttpHandler. The aspx html markup will look something like this.

<html xmlns=”; >
<head runat=”server”>
<title>Load ASP.Net User Control</title>
<script src=”jquery-1.2.6.js”></script>
$(document).ready(function() {
$(“#BtnLoadUserCtrl”).click(function() {
<form runat=”server”>
<br />
<input value=”Load User Control” /> <br />
<div id=”UserCtrl”></div>

The code is quite readable. On the click event of BtnLoadUserCtrl button, SampleUserCtrl.ascx user control is being tried to load in the <div> element having id UserCtrl.

Then, write our custom Httphandler called jQueryHandler as below.

public class jQueryHandler:IHttpHandler
public void ProcessRequest(HttpContext context)
// We add control in Page tree collection
using(var dummyPage = new Page())
context.Server.Execute(dummyPage, context.Response.Output, true);

private Control GetControl(HttpContext context)
// URL path given by load(fn) method on click of button
string strPath = context.Request.Url.LocalPath;
UserControl userctrl = null;
using(var dummyPage = new Page())
userctrl = dummyPage.LoadControl(strPath) as UserControl;
// Loaded user control is returned
return userctrl;

public bool IsReusable
get { return true; }

Do not miss to add this HttpHandler in the web.config.

<add verb=”*” path=”*.ascx” type=”JQUserControl.jQueryHandler, JQUserControl”/>

This web.config configuration tells that jQueryHandler will process request for file type having .ascx extension and methods all (GET, POST, etc). The type attribute value is something like:
type=”Namespace.TypeName, Assembly name where Handler can be found”

Now we are ready to test our sample. Run the page, and see on the click of button, the sampleusertCtrl.ascx is loaded.

I hope we can now extend this concept to fit any such programming requirement in future.
Happy Coding!