Sitefinity ASP.NET CMS - Content Management System

Support Forum Thread

Home >  Support >  Forums home > Sitefinity 3.x > Bugs & Issues > Writing HttpHandler

Reply
Zubair, 5/1/2008 6:33:56 AM

I am trying to write an HttpHandler for mapping the Old Urls which might be index in Google to new urls as follows

old
mywebsite.com/corporate
mywebsite.com/about

new
mywebsite.com/sections/corporate.aspx
mywebsite.com/sections/about.aspx

To do this, I'm writing an HttpHandler as follows

<div style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border: solid 1px gray; cursor: text; margin: 20px 0px 10px 0px; max-height: 200px; overflow: auto; padding: 4px 4px 4px 4px; width: 97.5%;"><div style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%;"><BR><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">   1:</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> OldUrlMapper : IHttpHandler</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">   2:</span> {</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">   3:</span>     <span style="color: #0000ff;">public</span> OldUrlMapper()</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">   4:</span>     {</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">   5:</span>         <span style="color: #008000;">//</span></pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">   6:</span>         <span style="color: #008000;">// TODO: Add constructor logic here</span></pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">   7:</span>         <span style="color: #008000;">//</span></pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">   8:</span>     }</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">   9:</span> &nbsp;</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  10:</span>     <span style="color: #cc6633;">#region</span> IHttpHandler Members</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  11:</span> &nbsp;</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  12:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> IsReusable</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  13:</span>     {</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  14:</span>         get {<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;}</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  15:</span>     }</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  16:</span> &nbsp;</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  17:</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> ProcessRequest(HttpContext context)</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  18:</span>     {</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  19:</span>         <span style="color: #0000ff;">if</span> (context.Request.Url.PathAndQuery.ToLower().IndexOf(<span style="color: #006080;">"this"</span>) &gt; 0)</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  20:</span>         {</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  21:</span>             context.Response.Redirect(<span style="color: #006080;">"~/contactus.aspx"</span>);</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  22:</span>             context.Response.End();</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  23:</span>         }</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  24:</span>     }</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  25:</span> &nbsp;</pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em;"><span style="color: #606060;">  26:</span>     <span style="color: #cc6633;">#endregion</span></pre><pre style="background-color: #f4f4f4; font-family: Consolas, 'Courier New', Courier, Monospace; font-size: 8pt; line-height: 12pt;  border-style: none; color: black; overflow: visible; padding: 0px 0px 0px 0px; width: 100%; margin: 0em; background-color: white;"><span style="color: #606060;">  27:</span> }</pre></div></div>

but this doesn't seem to work and pick any Urls, does it have something to do with all pages being virtual? please help


Reply
Zubair, 5/1/2008 6:36:32 AM
(sorry the old post has my code as HTML) 

I am trying to write an HttpHandler for mapping the Old Urls which might be index in Google to new urls as follows

old
mywebsite.com/corporate
mywebsite.com/about

new
mywebsite.com/sections/corporate.aspx
mywebsite.com/sections/about.aspx

To do this, I'm writing an HttpHandler as follows

/// <summary>

/// Summary description for OldUrlMapper

 

/// </summary>

 

public class OldUrlMapper : IHttpHandler

{

public OldUrlMapper()

{

//

 

// TODO: Add constructor logic here

 

//

 

}

 

#region IHttpHandler Members

public bool IsReusable

{

get {return true;}

}

public void ProcessRequest(HttpContext context)

{

if (context.Request.Url.PathAndQuery.ToLower().IndexOf("this") > 0)

{

context.Response.Redirect("~/contactus.aspx");

context.Response.End();

}

}

 

#endregion

 

}



but this doesn't seem to work and pick any Urls, I know I have registered the handler in web.config, does it have something to do with all pages being virtual? please help


Reply
, 5/2/2008 3:36:26 AM
Hi Zubair,

I have created a http handler with the code you've provided and I can see that in debugger it is entering the code, so you may want to show us how did you register the handler in web.config. The problem may be there. You can attach us the handler class and web.config and we'll inspect it.

However, there may be a better and simpler approach to your problem. First of all, Sitefinity supports multiple URL's, so you can have as many urls pointing to the same page. So you can keep old url's that were indexed by Google and also have the new URL's that you want to implement now.

As for having extensionless url's, please refer to our user manual on page 284 (section name "Extensionless URLs") and on page 287 (section name "Wildcard Application Mappings").

Namely, Sitefinity offers the feature you are trying to build out of box, so I think you may be better of taking advantage of that. Nevertheless, if you decide you want to implement custom HttpHanlder, we'll try to help you with that as well.

Sincerely yours,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
Zubair, 5/4/2008 7:15:08 AM

ok I was missing '*' in web.config <add verb="*" path="*.aspx"..... now I can hit a breakpoint inside the handler.

Now I notice that pages are opening really slowly with no content in them even if the condition is not met such as the keyword 'this' is not included in the Url in the following example.

if (context.Request.Url.PathAndQuery.ToLower().IndexOf("this") > 0)

{

context.Response.Redirect("~/contactus.aspx");

context.Response.End();

}

}

Please let me know what's wrong with it. Thanks.

Also why I chose to write an HttpHandler instead of using 'Add Url' property in SF is because I'm not sure that you do a 302 redirect (which Google doesn't follow) instead of a 301 redirect (which is Google-friendly) since the purpose of this handler is to redirect any old Google-indexed pages to new ones (I'll change Response.Redirect with Response.RedirectLocation later) see this link for details on this, it'll be nice if you do it out-of-the-box. my two cents.


 


Reply
, 5/5/2008 8:27:15 AM
Hi Zubair,

I've noticed that you've got the answer on the other ticket, but I'll just repeat it here for your convenience:

"I’d like to inform you that 301 Redirection for optional URLs has already been implemented and the feature will be available with the upcoming release."

Let us know if we can help you with anything else.

Sincerely yours,
Ivan
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
Zubair, 5/5/2008 8:51:38 AM
Yes thank you.
Reply
Zubair, 5/18/2008 7:10:57 AM
hi,

Haven't received any response to the issue above. I'm running into yet another issue.

I'm trying to implement Approach 1 from http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx and want to write my Urls as www.mywebsite.com/products.aspx/Recent or www.mywebsite.com/products/YourOrder etc

I noticed that ScottGu's approach works with all static pages I have in the application but it doesn't work with any virtual pages from SF, can you please elaborate on this and tell me why ?

Thanks.

Reply
, 5/22/2008 10:28:32 AM
Hello Zubair,

We went through ScottGu's Blog and the URL method and found out that it is pretty familiar to the one currently used in Sitefinity. Anyway, as a more reliable way of implementing this, we recommend using the URL rewriting approach explained in the Sitefinity User Manual. All relevant information is included in the URL Rewriting section.

Hope this helps.

Sincerely yours,
Nikifor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
Zubair, 5/25/2008 1:13:30 AM
hi Nikifor,

Thanks for replying, I went through the Url Rewriting section and setup the web.config appropriate rules but I notice that Url Rewriter is not kicking in as the new Url says "Cannot find resource".

I also tried recycling the app pool (saving the web.config should have resetted the app but anyway)

Can you explain why ?

Thanks.

Reply
, 5/26/2008 10:50:07 AM
Hi Zubair,

There are several known issues with the URL rewriter, but none is related to the error message which you receive. We tried reproducing the reported behavior but unfortunately we did not manage to do that. This is why it would be really helpful if you could explain what are the exact strings you are trying to rewrite in examples. Also, please send us your web.config so that we can verify whether your rules are correct.

Thank you for your cooperation.

Greetings,
Nikifor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
Zubair, 5/26/2008 11:55:52 PM
Are these known issues going to be fixed in SP2 ? is it coming out this month ?.

I've just added a support ticket further to this issue and posted my web.config file, please review and let me know what's going on.

Thanks

Reply
, 5/29/2008 6:45:28 AM
Hi Zubair,

As promised by us, the 301 redirect is already implemented and it will be part of Sitefinity 3.2 SP2. All other known issues related to the URL rewriting option are already fixed and will also be shipped in the upcoming release.

Please find the answer about the URL rewriting issue in the support thread.

Best wishes,
Nikifor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center

Reply
Zubair, 5/29/2008 6:51:02 AM
That's great news.

Yea I implemented the Url rewriting.

Thanks