We spent two days on this so I felt I had to tell the world how it was accomplished.
Issue:
Someone links to contact.htm on your site. You do not have a contact.htm, but you do have a contact.aspx.
Solution
Step 1: Configure IIS
What happens if you request a non-existent HTM,ASP page?
By default you will get a canned "white page". When you install the .NET Framework is maps certain file extensions to the ASP.NET ISAPI, aspnet_isapi.dll. Neither HTML nor HTM files are mapped to it (because they are not ASP.NET pages). However, you can configure IIS to treat them as ASP.NET pages and serve our custom error pages.
Run the IIS Manager
Select a web application
Right click and go to Properties
On the Virtual Directory tab click Configuration
In the Extension column find .aspx, double click and copy the full path to aspnet_isapi.dll. It should be something like C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\«
aspnet_isapi.dll
Click Add and paste the path in the Executable box
In the Extension box type .html
Make sure Check that file exists is NOT checked
Dismiss all dialogs
Step 2: Modify Web.Config
Add as many types as you want into the httphnadler section of your web.config.
<httpHandlers>
<add path="*.htm" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="*.html" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="*.asp" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
Step 3: Set Default Page Properties
- Open Sitefinity Admin
- Open the Contact.aspx file.
- Click Properties
- Expand More Options
- Add another URL (~/Contact.htm)
- Check the “Redriect to Default URL” box
- Save Changes.
Step 4: Write code using Telerik API
Add this bit of code to your global.asax
void Application_Error(object sender, EventArgs e)
{
Exception lastException = this.Server.GetLastError();
if (lastException is HttpException)
{
HttpException httpException = (HttpException)lastException;
if (httpException.GetHttpCode() == 404)
{
// get the name of the requested page - just the /thisPage.htm or /DIR/thisPage.asp etc
string pageName = string.Concat("~", Request.Url.AbsolutePath.ToString());
// create a new instance of CmsManager
Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
// let's get the page by passing one of it's additional url's
Telerik.Cms.IPage myPage = cmsManager.GetPageByAdditionalUrl(pageName);
// we can cast myPage as ICmsPage and redirect to the Default URL
if (myPage != null)
{
Response.Redirect(((Telerik.Cms.ICmsPage)myPage).DefaultUrl.Url);
this.Server.ClearError();
}
}
}
}
Now if you come to the site with contact.htm and it doesn’t exist, you will be redirected to contact.aspx.
I hope this helps