+1-888-365-2779
Try Now
More in this section

HttpHandler

5 posts, 0 answered
  1. Luc Baeten
    Luc Baeten avatar
    169 posts
    Registered:
    23 Sep 2005
    17 Mar 2011
    Link to this post
    Hello Sitefinity Team

    I am writing an HttpHandler for SEO url optimization.

    When I use it with a redirect like code below everything works fine.

    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System;
      
    /// <summary>
    /// Summary description for ProductHandler
    /// </summary>
    public class ProductHandler : IHttpHandler
    {
        /// <summary>
        /// Process my requet
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string strUrlPath = context.Request.RawUrl.ToString();
                int productSep = strUrlPath.ToLower().IndexOf("product__");
                // get the requested page name like article1.aspx
                string strUrl = System.IO.Path.GetFileName(strUrlPath);
                int len = strUrl.IndexOf(".");
                int sep = strUrl.LastIndexOf("__");
                // now get the exact id
                string productID = strUrl.Substring(sep + 2, strUrl.Length - len);
                string productUrl = strUrlPath.Substring(0, productSep) + "product.aspx?productno=" + productID;
      
                // Now transfer the request to the actual page that will show the article based on the id
                context.Response.Redirect(productUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
      
    }

    But in fact I don't want a redirect for SEO reasons: I will want to keep the originall url (product__RVS_INOX__Kaassnijder__68139.aspx), selected by the user and not the redirected url (product.aspx?productno=68139). So I tried to change the code this way

    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System;
      
    /// <summary>
    /// Summary description for ProductHandler
    /// </summary>
    public class ProductHandler : IHttpHandler
    {
        /// <summary>
        /// Process my requet
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string strUrlPath = context.Request.RawUrl.ToString();
                int productSep = strUrlPath.ToLower().IndexOf("product__");
                // get the requested page name like article1.aspx
                string strUrl = System.IO.Path.GetFileName(strUrlPath);
                int len = strUrl.IndexOf(".");
                int sep = strUrl.LastIndexOf("__");
                // now get the exact id
                string productID = strUrl.Substring(sep + 2, strUrl.Length - len);
                string productUrl = strUrlPath.Substring(0, productSep) + "product.aspx?productno=" + productID;
      
                Page page = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(productUrl, typeof(Page)) as Page;
                page.AppRelativeVirtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
                page.ProcessRequest(context);
      
                // Now transfer the request to the actual page that will show the article based on the id
                //context.Response.Redirect(productUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
      
    }

    But now I get an error "The resource cannot befound" when the productUrl points to a page in Sitefinity. When it points to a file on disk it does work.

    Can you tell me what I need to do to make it work for Sitefinity pages?

    Kind regards
    Luc Baeten


  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    19 Mar 2011
    Link to this post
    Hello Luc Baeten,

    We have a UrlRewritter built in, for this purpose. Please check the 3.x User's Manual, we have an example similar to yours.

    When you want to keep the original URL (the user friendly one), you should use Rewrite. Otherwise, you may go with a redirect.

    All the best,
    Georgi
    the Telerik team
  3. Luc Baeten
    Luc Baeten avatar
    169 posts
    Registered:
    23 Sep 2005
    20 Mar 2011
    Link to this post
    Thanks Georgi

    This was what I am looking for. I am able to keep the user friendly url with the UrlRewriter class.

    regards
    Luc
  4. Jenna Brenn
    Jenna Brenn avatar
    5 posts
    Registered:
    07 May 2010
    02 May 2011
    Link to this post
    Hello Georgi,

    I have a request similar to this in that I would like to write a custom HttpHandler for SEO url optimization, although I need to query my custom database in order to set up the url rewrite rules.  I am trying to take a url like "www.domain.com/product" and have it point to "www.domain.com/product.aspx?id=1234562945789" and in order to do that I need the HttpHandler to get the product id from the database.

    I guess that my question is - #1, is this possible - and #2 do you have an example of code that would do this or an addition to Luc's code from this post that will work?

    I have looked at the <urlrewrites> code for the web.config from the 3.7 user's manual (which works great for my other pages), but this won't work for me in this instance since I can't do a query and the product id isn't part of the SEO friendly url I want to use.

    Any help you could give me would be very appreciated!
    - Jenna
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    02 May 2011
    Link to this post
    Hello Jenna,

    Please check this forum post.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
5 posts, 0 answered