2

I have to map a nice looking ASP.NET MVC URL '/sel/F/61' to an ASPX url with query string parameters like 'familydrilldown.aspx?familyid=61'. I tried to add this to global.asax:

routeTable.MapPageRoute(
                "selectorroute", 
                "sel/F/{familyid}", 
                "~/selector/familydrilldown.aspx");

which works, except that the familyid is not passed as a querystring to familydrilldown.aspx.

How can I take the {familyid} and pass it as a querystring parameter to the page familydrilldown.aspx?

I tried this:

routeTable.MapPageRoute(
                "selectorroute", 
                "sel/F/{familyid}", 
                "~/selector/familydrilldown.aspx?familyid={familyid}");

but of course this doesn't really work...

Any ideas on how to do this?

Thanks!

1
  • I ran into exactly the same issue. It sure would be nice if the syntax you used above actually worked! As is, I've got a lot of code that looks first for a query string, then for route data. Maybe I'll work on a generic version of Lud's solution if I get bored some day. Commented Nov 21, 2011 at 15:03

2 Answers 2

3

I found a solution by implementing my own IRouteHandler:

public class SelectorRouteHandler<T> : IRouteHandler 
    where T : IHttpHandler, new()
{                                      
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string queryString = 
            "?familyid=" + requestContext.RouteData.Values["familyid"];

        HttpContext.Current.RewritePath(
            string.Concat("~/selector/familydrilldown.aspx", queryString));

        var page = BuildManager.CreateInstanceFromVirtualPath(
            "~/selector/familydrilldown.aspx", typeof(T)) as IHttpHandler;

        return page;                    
    }
}

and registering it like:

routeTable.Add(
    "selectorRoute", 
    new Route("sel/F/{familyid}", SelectorRouteHandler<Page>()));
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but just a note, you can also get at the HttpContext object via requestContext.HttpContext. I imagine that the two implementations are roughly equivalent, but to me, it feels better to use the requestContext object.
0

I don't know if what you want is possible. Why don't you just use?

var familyId = Page.RouteData.Values["familyid"]

in the Page_Load (there might be some casting involved)

1 Comment

Thank you for your comment. The aspx pages already exist, so it would be nice if we don't need to change them... and currently they use the Request.QueryString object...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.