Click here to Skip to main content
15,886,065 members
Articles / Web Development / ASP.NET
Article

Custom File Extensions in ASP .NET - The Easy and Manageable Way

Rate me:
Please Sign up or sign in to vote.
1.19/5 (4 votes)
30 May 2008CPOL2 min read 20.5K   9   2
Custom file extensions such as Default.sample can be created very easily without the need to configure IIS.

Introduction

Custom file extensions in ASP .Net pages are very easy to create. Over the course of learning and keeping myself up to date on the numerous changes that Microsoft keeps on implementing to the .NET Framework, this is probably one the easiest techniques to implement. Nonetheless, it has also been a source of confusion for many, partly because of the reason that most tutorials on this topic deal with adding mappings in IIS. But, is there another way? May be you do not want to play around with IIS settings, or you just do not have IIS setup.

Using the code

This tutorial discusses a very simple technique to implement custom file extensions (such as default.sample, or default.ecubicle) in to your web application. A step by step solution is presented and then discussed.

STEP BY STEP

a) Add a new class to your project by right clicking on the Add New Item Node of your Web App. Name the class

b) The class must implement IHTTPHANDLER interface. Therefore the definition of class should look like:

VB.NET
Public Class Class1 Implements IHttpHandler

c) Visual Studio would automatically fill the source with IsReusable property and ProcessRequest method.

VB.NET
Public ReadOnly Property IsReusable() As Boolean _
                          Implements System.Web.IHttpHandler.IsReusable
Get
End Get
End Property

Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) _
                          Implements System.Web.IHttpHandler.ProcessRequest 
End Sub 

d) Add the following code to PrcessRequest

ASP.NET
context.Server.Transfer("~/Default2.aspx") 


e) Add the following code in Get section of the IsReusable property

VB.NET
Return False 

f) In Web.Config file, find <httphandlers> section and add this code. Customextension is any extension that you wish to use.

XML
<add verb="*" type="Class1" path="*.customextension"/> 

g) Now, create a default2.aspx. This default2.aspx is your main page and you can do whatever you want here.

h) In the end, add a blank web form named default.customextension and set this form as your web site's startup by going to WebSite -> Start Options and selecting this page. You would not be able to set this as the web site's startup page by right clicking on it because the extension of the file is not .ASPX. But, you can do this by going into Website -> Start Options and selecting default.customextension page.

Explanation:

What this code does is to implement HTTPHANDLER interface and intercept any requests to our custom extension defined in web.config file. Once, this is done, our ProcessRequest method calls server.transfer to redirect the request to another page. Server object’s transfer method is the key here because if we were to use Response.Redirect, the user’s could then see the file name (which in this case ends in .aspx) in the address bar of the browser.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) eCubicle Solutions
Canada Canada
http://www.ecubicle.net

Comments and Discussions

 
QuestionUsage scenario? Pin
Chris Maunder31-May-08 12:20
cofounderChris Maunder31-May-08 12:20 
AnswerRe: Usage scenario? Pin
Rafay Bin Ali31-May-08 12:50
Rafay Bin Ali31-May-08 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.