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

发布于 2024-11-09 09:07:07 字数 700 浏览 5 评论 0原文

我必须将一个漂亮的 ASP.NET MVC URL '/sel/F/61' 映射到带有查询字符串参数(如 'familydrilldown.aspx?familyid=61')的 ASPX URL。我尝试将其添加到 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}");

但是当然这并没有真正起作用...

关于如何做到这一点有什么想法吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最近可好 2024-11-16 09:07:07

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

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>()));
孤君无依 2024-11-16 09:07:07

我不知道你想要的是否可以实现。
你为什么不直接使用呢?

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

在 Page_Load 中(可能涉及一些转换)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文