使用查询字符串将ASP.NET MVC URL路由到url

时间:2011-05-23 15:36:31

标签: asp.net-mvc forms asp.net-mvc-routing

我必须将一个漂亮的ASP.NET MVC URL'/ sel / F / 61'映射到一个ASPX url,其中包含'familydrilldown.aspx?familyid = 61'等查询字符串参数。我试着将它添加到global.asax:

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

有效,但familyid不作为查询字符串传递给familydrilldown.aspx。

如何将{familyid}作为查询字符串参数传递给页面familydrilldown.aspx?

我试过了:

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

但当然这不起作用......

关于如何做到这一点的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:2)

我通过实现自己的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;                    
    }
}

并将其注册为:

routeTable.Add(
    "selectorRoute", 
    new Route("sel/F/{familyid}", SelectorRouteHandler<Page>()));

答案 1 :(得分:0)

我不知道你想要的是否可能。 你为什么不用?

var familyId = Page.RouteData.Values["familyid"]
Page_Load中的

(可能涉及一些演员)