Rewriting the URL using IHttpHandlerFactory

Last post 06-28-2009 9:11 PM by mcleanap. 0 replies.

Sort Posts:

  • Rewriting the URL using IHttpHandlerFactory

    06-28-2009, 9:11 PM
    • Member
      3 point Member
    • mcleanap
    • Member since 06-01-2009, 11:18 PM
    • Posts 41

    I am using the following post (http://www.uberasp.net/getarticle.aspx?id=49) to process url rewrites on our website.  I am fairly new to .NET development, so I was curious if this is a good way to do the URL rewrite. 

    1. Would there be any issues with post back?  I don't see that there are.

    2. Would these URLs be properly indexed by search engines?

    3. Any other issues that I should be aware of?

    I have modified the code somewhat to hopefully fit my needs.  Do you see any issues with it?


    In the web.config file, I changed it to look for any file in the root with an aspx extension.

    <add verb="*" path="*.aspx" type="MyPageFactory" />

    In the MyPageFactory class, I fill a list of pages that I wouldn't want the URL rewrite to apply to.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    
    public class MyPageFactory : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            string pageName = "~/Default.aspx";
    
            List<string> pageList = new List<string>();
    
            pageList.Add("contact");
            pageList.Add("login");
            pageList.Add("forgotpassword");
    
            context.Items["fileName"] = Path.GetFileNameWithoutExtension(url).ToLower();
            string fileName = context.Items["fileName"].ToString();
    
            if (pageList.Exists(delegate(string s) { return s == fileName; }))
            {
                pageName = "~/" + fileName + ".aspx";
            }
            else
            {
                pageName = "~/Default.aspx";
            }
    
            return PageParser.GetCompiledPageInstance(url, context.Server.MapPath(pageName), context);
        }
    
        public void ReleaseHandler(IHttpHandler handler)
        {
    
        }
    }
    
    
    




Page 1 of 1 (1 items)