Routing ASP.NET MVC URL to url with querystring

夙愿已清 提交于 2019-12-11 03:28:40

问题


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 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>()));



回答2:


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)



来源:https://stackoverflow.com/questions/6099459/routing-asp-net-mvc-url-to-url-with-querystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!