How to Access GetRouteUrl in .ashx IHttpHandler's


For a while I wasn't sure how to access GetRouteUrl from an .ashx IHttpHandler page.   I wanted to access route url's setup in the Global.asax file to be used in files like rss.ashx,  instead of having to hard codes the page URL's in my .ashx pages.  Well, this routing problem isn't so difficult once you know how to do it.  To access and use GetRouteUrl in an .ashx follow this simple example:

string url = new System.Web.UI.Control().GetRouteUrl("ArticleStory", new { pathname = pathName });

It's as simple as that...!  You'll just need to change the routeName, and object parameters to your specific Route.

Also if you need to get fully qualified URL's see: How to Get Fully Qualified Route Urls from GetRouteUrl



Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts

The new Routing features in ASP.NET 4.0 are pretty awesome. However, one issue I ran into recently was trying to get the fully qualified urls from Page.GetRouteUrl as string urls to be used in emails messages. Unfortunately, I wanted something that worked...  more »

If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL. The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the...  more »