Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
30 May 2008 1  
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:

Public Class Class1 Implements IHttpHandler

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

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

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


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

Return False 

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

<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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here