Click here to Skip to main content
Licence CPOL
First Posted 30 May 2008
Views 9,269
Bookmarked 9 times

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

By | 30 May 2008 | Article
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rafay Bin Ali

Software Developer (Senior)
eCubicle Solutions
Canada Canada

Member

http://www.ecubicle.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUsage scenario? PinadminChris Maunder12:20 31 May '08  
AnswerRe: Usage scenario? PinmemberRafay Bin Ali12:50 31 May '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 30 May 2008
Article Copyright 2008 by Rafay Bin Ali
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid