URL查询字符串与给url

URL查询字符串与给url

本文介绍了路由ASP.NET MVC URL查询字符串与给url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要与查询字符串参数,如familydrilldown.aspx?FAMILYID = 61'图个好看的ASP.NET MVC URL'/ SEL / F / 61'到一个ASPX URL。我试图将它添加到Global.asax中:

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

其中工程,除了FAMILYID不是作为查询字符串到familydrilldown.aspx通过。

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

我怎么能走{} FAMILYID,并传递作为查询字符串参数页面familydrilldown.aspx?

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

我试过这样:

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?

谢谢!

推荐答案

我发现通过实现我自己的IRouteHandler一个解决方案:

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;
    }
}

和注册它,如:

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

这篇关于路由ASP.NET MVC URL查询字符串与给url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:11