2

I'm using ASP.NET routing to map URLs to some arbitrary number of sections to my site. They will all be handled by the same page, though I want to have different URLs. For example

http://site.com/blog for my blog section
http://site.com/mysection for mysection etc.

There can be any number of those sections, but they will all be handled by Lister.aspx. I also have another page that handles the content, named ContentView.aspx which shows off content. My route is as follows: (cats is list of categories)

cats.ForEach(c =>
        {
            table.Add(new Route(c.ShortName, new PageRouteHandler("~/Lister.aspx?cat=" + c.ID), ));
            table.Add(new Route(c.ShortName + "/{id}", new PageRouteHandler("~/ContentView.aspx")));
            table.Add(new Route(c.ShortName + "/{id}/{title}", new PageRouteHandler("~/ContentView.aspx")));
        });

Let's say I have a blog post with ID 123 and title hello:
A user can access it by http://site.com/blog/123/hello
They can also use just the ID http://site.com/blog/123
As you may guess, I want http://site.com/blog to point to the lister page instead of a content viewer, and I also need the category ID to distinguish which category (blog or mycategory or anything else that was created dynamically) I should display. I'm trying to pass the category ID to the querystring, the lister page is called, but with no query string. Why is this behavior, and how can I send my category ID to the lister page?

1 Answer 1

1

I would suggest creating your own RouteHandler that implements IRouteHandler which you could then pass a separate parameter to its constructor (the category ID).

Then in the GetHttpHandler you could then pass that ID on to the Page using HttpContext.

Something like:

public class CategoryRouteHandler : IRouteHandler 
{
   private string _virtualPath;
   private string _category;

   public CategoryRouteHandler(string virtualPath, string category)
   {
       _virtualPath = virtualPath;
       _category = category;
   }

   public IHttpHandler GetHttpHandler(RequestContext requestContext) 
   {
        var handler = BuildManager.CreateInstanceFromVirtualPath(
            _virtualPath, typeof(Page)) as IDefaultHttpHandler;

        HttpContext.Current.Items["Category"] = _category;

        return handler;
   }
} 

Then you can use:

table.Add(new Route(c.ShortName, new CategoryRouteHandler("~/Lister.aspx", c.ID), ));

And from the code of Lister you can access HttpContext.Current.Items["Category"]

Edit: updated to fix the code, sorry I didn't test it the first time.

Sign up to request clarification or add additional context in comments.

1 Comment

your code contains two errors: what is the base class of CategoryRouteHandler and what does GetHttpHandler method return? I tried to get it working by using PageRouteHandler as base class and returning the current IHttpHandler but could't pass the value to the page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.