Welcome to the third entry in my series of posted dedicated to answering Scott Hanselman’s ASP.NET interview questions.  In this entry, we will tackle using custom extensions (Scott uses *.jsp as an example to preserve backwards compatibility) to serve up ASP.NET pages.  By custom extensions, I mean that if you go to http://localhost/default.jsp you can actually direct the webserver to process the request using the ASP.NET worker process.

As it turns out, custom extensions are not all that difficult in ASP.NET assuming you have no aversions to getting under the hood and making some changes to IIS.  It also ties back nicely to the last post I did in this series that covered ASHX files and HttpHandlers.

To start out, lets first instruct IIS to send any request ending in a *.jsp extension to be passed on to the ASP.NET worker process.  To do this, open up Internet Information Services and find the website on your machine that you want to configure:

IIS


Next, select "Configuration" on the Properties page:

Properties


Here, you will be able to add the custom *.jsp extension.  For ease of use, I just copied the configuration values from the *.aspx extension.

Configuration


Extension


Click "OK" until you are back to the main IIS screen and then exit.  At this point, IIS knows that when it receives a request with a *.jsp extension to pass the request along to the ASP.NET worker process.  Next, we need to instruct our application to process these incoming requests using the PageHandlerFactory HttpHandler.  To do this, open up the web.config file and add the following lines to the httpHandlers section in the system.web node:

<httpHandlers>
 
<add path="*.jsp" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true" />
</
httpHandlers>

Also, you’ll need to add the following lines to the compilation node (again in the system.web node):

<buildProviders>
 
<add extension=".jsp" type="System.Web.Compilation.PageBuildProvider" />
</
buildProviders>

Visual Studio 2005 automatically creates the compilation node as self-closing, so here is what it should look like after you’ve added the lines above:

<compilation debug="true" strict="false" explicit="true">
 
<buildProviders>
   
<add extension=".jsp" type="System.Web.Compilation.PageBuildProvider" />
  </
buildProviders>
</compilation>

Now, IIS knows how to pass the request along and your application knows how to process any request with a *.jsp extension.  At this point, we’re basically finished.  If you already have an *.aspx page that you’d like to serve up with a *.jsp extension, all that’s left is to rename it and rebuild your application.  Otherwise, I’ve found it easier to create your pages uses the *.aspx extension and then change it to *.jsp once you’re finished making your code changes.

Here is a screenshot from an application that I configured on my machine before the Page PostBack:

JspLoad


And after the PostBack:

JspPostBack


If you’d like, I also zipped up the solution which can be downloaded here.  If you have any additional questions, please feel free to leave them in the comments!